Mybatis 开启二级缓存,使用 Redis 去实现 cache 进行缓存处理,这样就不用一直去请求数据库,降低数据库的压力,并且查询也会快一点
设置mybatis 开启二级缓存
<setting name="cacheEnabled" value="true"/>
Redis 工具类:
public class JedisPoolUtil {
private JedisPoolUtil() {
}
public static volatile JedisPool jedisPool = null;
public static JedisPool getJedisPoolInstance(){
if(null == jedisPool){
synchronized (JedisPoolUtil.class) {
if(null == jedisPool){
JedisPoolConfig poolConfig = new JedisPoolConfig();
// poolConfig.setMaxActive(1000);
poolConfig.setMaxIdle(32);
// poolConfig.setMaxWait(1000*100);
poolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(poolConfig ,"127.0.0.1",6379,100,"root");
}
}
}
return jedisPool;
}
}
RedisCache: 实现cache
public class RedisCache implements Cache {
private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
// cache instance id
private final String id;
public RedisCache(String id) {
this.id = id;
}
@Override
public String getId() {
return id;
}
@Override
public void putObject(Object key, Object value) {
try {
JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
logger.debug("put cached query result from redis");
jedisPoolInstance.getResource().set(key.toString(),value.toString());
} catch (Throwable t) {
logger.error("Redis put failed, fail over to db", t);
}
}
@Override
public Object getObject(Object key) {
try {
JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
logger.debug("Get cached query result from redis");
return jedisPoolInstance.getResource().get(key.toString());
} catch (Throwable t) {
logger.error("Redis get failed, fail over to db", t);
return null;
}
}
@Override
public Object removeObject(Object key) {
try {
JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
logger.debug("remove cached query result from redis");
return jedisPoolInstance.getResource().del(key.toString());
} catch (Throwable t) {
logger.error("Redis remove failed, fail over to db", t);
return null;
}
}
@Override
public void clear() throws CacheException {
try {
JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
logger.debug("clear cached query result from redis");
jedisPoolInstance.getResource().flushDB();
} catch (Throwable t) {
logger.error("Redis clear failed, fail over to db", t);
}
}
@Override
public int getSize() {
return 0;
}
@Override
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
}
在xml中使用 :
<cache type="cn.jeeweb.core.cache.RedisCache">
<property name="eviction" value="LRU"/>
<property name="flushInterval" value="60000000"/>
<property name="size" value="2048"/>
<property name="readOnly" value="false"/>
</cache>
最后,在过程中出现过一些小的错误,希望记录下来
Caused by: java.lang.ClassCastException: cn.jeeweb.core.cache.RedisCache cannot be cast to org.apache.ibatis.cache.Cache
这个错误,是因为项目中时用 shiro 做权限框架,导致导入cache包时导入错误,应该导入 org.apache.ibatis.cache.Cache 包才对
Caused by: java.lang.NoSuchMethodException: cn.jeeweb.core.cache.RedisCache.<init>(java.lang.String)
这个是因为cache 需要一个构造id