Mybatis开启分布式缓存
- 导入相关依赖
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
- 创建工厂类
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
*lzl
*/
public class RedisUtil {
//服务器IP地址
private static String ADDR = "192.168.50.171";
//端口
private static int PORT = 6379;
//密码
private static String AUTH = "admin@123";
//连接实例的最大连接数 -1为不限制
private static int MAX_ACTIVE = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
private static int MAX_WAIT = 10000;
//连接超时的时间
private static int TIMEOUT = 10000;
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
//数据库模式是16个数据库 0~15
public static final int DEFAULT_DATABASE = 0;
/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT,AUTH,DEFAULT_DATABASE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Jedis实例
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
log.debug("The jedisPool is running now!");
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/***
*
* 释放资源
*/
public static void returnResource(final Jedis jedis) {
if(jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
- 以上基本就可以使用了,但是如果是要存对象,则需要序列化工具
package com.kando.util;
/**
*lzl
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import org.apache.ibatis.cache.CacheException;
public final 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) {
throw new CacheException(e);
}
}
public static Object deserialize(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) {
throw new CacheException(e);
}
}
}
- 新建一个类继承Cache,并且重写方法
package com.kando.configuration;
import com.kando.util.RedisUtil;
import com.kando.util.SerializeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
*lzl
*/
@Slf4j
public class RedisCache implements Cache {
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private String id;
public RedisCache(String id) {
this.id = id;
}
@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
@Override
public String getId() {
return UUID.randomUUID().toString();
}
@Override
public void putObject(Object key, Object value) {
Long mybatisCache = null;
try {
Jedis jedis = RedisUtil.getJedis();
mybatisCache = jedis.hset(("mybatisCache:"+id).getBytes("utf-8"),turnKey(key),SerializeUtil.serialize(value));
jedis.expire("mybatisCache:"+id,36000);
jedis.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
log.debug("putObject is putting #{}",mybatisCache);
}
@Override
public Object getObject(Object key) {
Object result =null;
try {
Jedis jedis = RedisUtil.getJedis();
byte[] bytes = jedis.hget(("mybatisCache:"+id).getBytes("utf-8"), turnKey(key));
jedis.expire("mybatisCache:"+id,36000);
jedis.close();
result = SerializeUtil.deserialize(bytes);
} catch (Exception e) {
e.printStackTrace();
}
log.debug("is doing getObject");
return result;
}
@Override
public Object removeObject(Object key) {
Long mybatisCache = null;
try {
Jedis jedis = RedisUtil.getJedis();
mybatisCache = jedis.hdel(("mybatisCache:"+id).getBytes("utf-8"),turnKey(key));
jedis.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return mybatisCache;
}
@Override
public void clear() {
try {
Jedis jedis = RedisUtil.getJedis();
jedis.del(("mybatisCache:"+id).getBytes("utf-8"));
jedis.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
log.debug("delete mybatisCache.");
}
@Override
public int getSize() {
int mybatisCache = 0;
try {
Jedis jedis = RedisUtil.getJedis();
mybatisCache = jedis.hgetAll(("mybatisCache:"+id).getBytes("utf-8")).size();
jedis.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
log.debug("is doing getSize ,the values is "+ mybatisCache);
return mybatisCache;
}
public byte[] turnKey(Object object) throws UnsupportedEncodingException {
if(object==null){
return null;
}
String str = object.toString();
// String[] split = str.split(":");
// String newKey = split[2]+":"+split[5]+":"+split[6]+":"+split[7];
return str.getBytes("utf-8");
}
}
- 最后在xml文件里开启缓存,并且指定自定义cache
<cache eviction="LRU" type="com.kando.configuration.RedisCache" />
- 有需要实时刷新的方法,则需要加入下面属性
flushCache="true"
注意
得到redis客户端的方式为
Jedis jedis = RedisUtil.getJedis();
jedis.dosomething......
//close()方法不是关闭连接,是将连接返回给连接池,一定要返回
jedis.close();
网友评论