redis实现mybatis二级缓存

在前面的文章中已经看到了mybatis二级缓存的设置,这里我们重新学习一下,mybatis 的二级缓存可以使用mybatis 自身的hashMap实现二级缓存,而如果使用mybatis 自身的二级缓存则很方便在映射文件中添加</cache> 就开启了mybatis 的二级缓存也可简单设置一下二级缓存的一些属性,具体如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis1.userMap">
	<cache eviction="FIFO" flushInterval="600000" size="1024"
		readOnly="true" />
	<resultMap id="result" type="com.mybatis1.User">
		<result property="name" column="name" />
		<result property="password" column="password" />
	</resultMap>
	<select id="getUser" parameterType="java.lang.String" resultMap="result">
		select * from usert where name =#{name}
	</select>
	<insert id="addUser" parameterType="com.mybatis1.User">
		insert into usert values(#{name},#{password})
	</insert>
</mapper>

其中的<cache /> 标签中设置了mybatis 在这个namespace (命名空间下) 的二级缓存的收集方式、存活时间、二级缓存大小、以及是否只读等属性

ok 上面的这些配置都是使用了mybatis 自身的二级缓存下面我们来看看使用第三方的缓存数据库来实现mybatis 的二级缓存:

mybatis 给我们提供了一个cache 接口来使用第三方的缓存,只需要实现这个接口就可以了!

下面我们先来看看 代码:

1、实现 cache 接口的代码:(第一种)

package com.mybatis3;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author WHD data 2016年5月22日
 */
public class RedisCache implements Cache {
	private Jedis redisClient = createReids();
	/** The ReadWriteLock. */
	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

	private String id;

	public RedisCache(final String id) {
		if (id == null) {
			throw new IllegalArgumentException("Cache instances require an ID");
		}
		this.id = id;
	}

	@Override
	public String getId() {
		return this.id;
	}

	@Override
	public int getSize() {
		// 获取 缓存key的大小
		return Integer.valueOf(redisClient.dbSize().toString());
	}

	@Override
	public void putObject(Object key, Object value) {
		System.err.println("put key" + key);
		redisClient.set(SerializeUtil.serialize(key.toString()),
				SerializeUtil.serialize(value));
	}

	@Override
	public Object getObject(Object key) {
		System.out.println("get key");
		Object value = SerializeUtil.unserialize(redisClient.get(SerializeUtil
				.serialize(key.toString())));
		return value;
	}

	@Override
	public Object removeObject(Object key) {
		System.err.println("remove key" + key);
		return redisClient.expire(SerializeUtil.serialize(key.toString()), 0);
	}

	// 在进行 insert update delete 时执行
	@Override
	public void clear() {
		System.err.println("clear all data");
		redisClient.flushDB();
	}

	@Override
	public ReadWriteLock getReadWriteLock() {
		return readWriteLock;
	}

	protected static Jedis createReids() {
		JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379);
		return pool.getResource();
	}
}

2、实现 cache 接口的代码:(第二种,copy的)

package com.mybatis3;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * @author WHD data 2016年5月22日
 */
public class MybatisRedisCache implements Cache {
	/** The ReadWriteLock. */
	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

	private String id;

	public MybatisRedisCache(final String id) {
		System.out.println("初始化");
		if (id == null) {
			throw new IllegalArgumentException("必须传入ID");
		}
		this.id = id;
	}

	@Override
	public String getId() {
		return this.id;
	}

	@Override
	public int getSize() {
		Jedis jedis = null;
		JedisPool jedisPool = null;
		int result = 0;
		boolean borrowOrOprSuccess = true;
		try {
			jedis = CachePool.getInstance().getJedis();
			jedisPool = CachePool.getInstance().getJedisPool();
			result = Integer.valueOf(jedis.dbSize().toString());
		} catch (JedisConnectionException e) {
			borrowOrOprSuccess = false;
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return result;

	}

	@Override
	public void putObject(Object key, Object value) {
		System.out.println("put key");
		Jedis jedis = null;
		JedisPool jedisPool = null;
		boolean borrowOrOprSuccess = true;
		try {
			jedis = CachePool.getInstance().getJedis();
			jedisPool = CachePool.getInstance().getJedisPool();
			jedis.set(SerializeUtil.serialize(key.hashCode()),
					SerializeUtil.serialize(value));
		} catch (JedisConnectionException e) {
			borrowOrOprSuccess = false;
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}

	}

	@Override
	public Object getObject(Object key) {
		System.out.println("get key");
		Jedis jedis = null;
		JedisPool jedisPool = null;
		Object value = null;
		boolean borrowOrOprSuccess = true;
		try {
			jedis = CachePool.getInstance().getJedis();
			jedisPool = CachePool.getInstance().getJedisPool();
			value = SerializeUtil.unserialize(jedis.get(SerializeUtil
					.serialize(key.hashCode())));
		} catch (JedisConnectionException e) {
			borrowOrOprSuccess = false;
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return value;
	}

	@Override
	public Object removeObject(Object key) {
		System.out.println("remove key 到了设定时间");
		Jedis jedis = null;
		JedisPool jedisPool = null;
		Object value = null;
		boolean borrowOrOprSuccess = true;
		try {
			jedis = CachePool.getInstance().getJedis();
			jedisPool = CachePool.getInstance().getJedisPool();
			value = jedis.expire(SerializeUtil.serialize(key.hashCode()), 0);
		} catch (JedisConnectionException e) {
			borrowOrOprSuccess = false;
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return value;
	}

	@Override
	public void clear() {
		System.out.println("clear all data");
		Jedis jedis = null;
		JedisPool jedisPool = null;
		boolean borrowOrOprSuccess = true;
		try {
			jedis = CachePool.getInstance().getJedis();
			jedisPool = CachePool.getInstance().getJedisPool();
			jedis.flushDB();
			jedis.flushAll();
		} catch (JedisConnectionException e) {
			borrowOrOprSuccess = false;
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
	}

	@Override
	public ReadWriteLock getReadWriteLock() {
		return readWriteLock;
	}

	
	public static class CachePool {
		JedisPool pool;
		private static final CachePool cachePool = new CachePool();

		public static CachePool getInstance() {
			return cachePool;
		}

		private CachePool() {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxIdle(100);
			config.setMaxWait(1000l);
			/*
			 * PropertiesLoader pl = new
			 * PropertiesLoader("classpath:config/redis.properties");
			 */
			// jedisPool= new JedisPool(config,"127.0.0.1",6379);
			pool = new JedisPool(config, "127.0.0.1", 6379);
		}

		public Jedis getJedis() {
			Jedis jedis = null;
			boolean borrowOrOprSuccess = true;
			try {
				jedis = pool.getResource();
			} catch (JedisConnectionException e) {
				borrowOrOprSuccess = false;
				if (jedis != null)
					pool.returnBrokenResource(jedis);
			} finally {
				if (borrowOrOprSuccess)
					pool.returnResource(jedis);
			}
			jedis = pool.getResource();
			return jedis;
		}

		public JedisPool getJedisPool() {
			return this.pool;
		}

	}
}

3、序列化反序列化类:

package com.mybatis3;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author WHD data 2016年5月22日
 */
public class SerializeUtil {

	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Object unserialize(byte[] bytes) {
		if (bytes == null)
			return null;
		ByteArrayInputStream bais = null;
		try {
			// 反序列化
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

4、User 对象

package com.mybatis3;

import java.io.Serializable;

/**
 * @author WHD data 2016年2月28日
 */
public class User implements Serializable {
	private static final long serialVersionUID = 3574211222602302068L;
	private String name;
	private String password;

	public User() {
	}

	public User(String name, String password) {
		this.name = name;
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "[name=" + name + ", password=" + password + "]";
	}

}

5、mybatis sql 映射文件:(使用第一种cache 实现类)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.userMap">
<cache eviction="FIFO" flushInterval="600000" size="1024" readOnly="true" 
		 type="com.mybatis3.RedisCache"/>
	<resultMap id="result" type="com.mybatis3.User">
		<result property="name" column="name" />
		<result property="password" column="password" />
	</resultMap>
	<select id="userInfo" parameterType="java.lang.String"
		resultMap="result" >
		select * from usert where name =#{name}
	</select>
	<insert id="insertInfo" parameterType="com.mybatis3.User">
	 insert into  usert (name,password) values(#{name},#{password})
	</insert>
</mapper>

5、mybatis sql 映射文件:(使用第二种cache 实现类)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.userMap">
<cache eviction="FIFO" flushInterval="600000" size="1024" readOnly="true" 
		 type="com.mybatis3.MybatisRedisCache"/>
	<resultMap id="result" type="com.mybatis3.User">
		<result property="name" column="name" />
		<result property="password" column="password" />
	</resultMap>
	<select id="userInfo" parameterType="java.lang.String"
		resultMap="result" >
		select * from usert where name =#{name}
	</select>
	<insert id="insertInfo" parameterType="com.mybatis3.User">
	 insert into  usert (name,password) values(#{name},#{password})
	</insert>
</mapper>

6、测试类: 注释了insert 操作

package com.mybatis3;

import java.io.InputStream;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis3.User;

/**
 * @author WHD data 2016年5月18日
 */
public class Test {
	public Test() {
	}

	public static void main(String[] args) {
		SqlSessionFactory factory = null;
		factory = getSession(null, null);
		SqlSession session = factory.openSession(ExecutorType.REUSE);
		SqlSession session1 = factory.openSession(ExecutorType.REUSE);
		User user = session.selectOne("com.mybatis3.userMap.userInfo", "whd");
		session.commit();
		User user2 = new User();
		user2.setName("hello");
		user2.setPassword("hello");
		// session1.insert("com.mybatis3.userMap.insertInfo", user2);
		session1.commit();
	}

	public static SqlSessionFactory getSession(String path, String environment) {
		if (null == path || "".equals(path)) {
			path = "config.xml";
		}

		InputStream input = Test.class.getClassLoader().getResourceAsStream(
				path);
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		// 其实这里的environment 可以从input中解析出来在对比如果不在这里的话可以抛个异常
		if (null == environment || "".equals(environment)) {
			environment = "development";
		}
		SqlSessionFactory factory = builder.build(input, environment);
		return factory;
	}
}

下面看看第一次和第二次执行的日志输出:

第一次执行:

初始化
get key
2016-05-23 21:27:07,737 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@22ad520d]
  2016-05-23 21:27:07,738 [main] DEBUG [java.sql.Connection] - ==>  Preparing: select * from usert where name =? 
  2016-05-23 21:27:07,811 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: whd(String)
  put key
进行了数据库查询并把查询的结果保存到了redis中

第二次执行:

初始化
get key

从结果我们就可以看到第二次是直接从redis中获取的数据而非数据库查询!

ok 这两次的执行都是在注解了 insert 操作后的, 下面我们来看看有insert 操作的执行结果:


第一次执行结果:

初始化
get key
2016-05-23 21:31:31,390 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@20e5e544]
  2016-05-23 21:31:31,391 [main] DEBUG [java.sql.Connection] - ==>  Preparing: select * from usert where name =? 
  2016-05-23 21:31:31,458 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: whd(String)
  put key
2016-05-23 21:31:31,517 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@66a7d863]
  2016-05-23 21:31:31,517 [main] DEBUG [java.sql.Connection] - ==>  Preparing: insert into usert (name,password) values(?,?) 
  2016-05-23 21:31:31,517 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: hello(String), hello(String)
  clear all data

第二次执行结果:

初始化
get key
2016-05-23 21:32:33,390 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@20e5e544]
  2016-05-23 21:32:33,391 [main] DEBUG [java.sql.Connection] - ==>  Preparing: select * from usert where name =? 
  2016-05-23 21:32:33,450 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: whd(String)
  put key
2016-05-23 21:32:33,511 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@66a7d863]
  2016-05-23 21:32:33,511 [main] DEBUG [java.sql.Connection] - ==>  Preparing: insert into usert (name,password) values(?,?) 
  2016-05-23 21:32:33,512 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: hello(String), hello(String)
  clear all data

从两次执行的结果我们看到,两次执行都先去redis缓存中获取数据,但都没有数据就进行了数据库的查询,查询后将查询的结果放在了redis中,但是在执行了insert 后mybatis 把redis缓存中的所有数据都清除了,所以第二次进行查询时还是从数据库中查询获取!


简单总结:

1、mybatis的二级缓存的范围是命名空间(namespace)

2、只要这个命名空间下有一个 insert、update、delete mybatis 就会把这个命名空间下的二级缓清空。

3、如果同一个sql在不同的命名空间下,就会出现脏数据,因为一个insert、update、deleted 了另一个可能还使用者缓存数据,这样就会出现数据的不一致性。

4、如果更新、删除、插入的频率比较高的话,就会删除所有缓存在添加所有缓存在删除,这样缓存的命中率很低或者说根本就起不到缓存作用而且会消耗资源。

所以在没解决这个问题的前提下,还是不提倡使用二级缓存。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值