/**
*因为rediscache本身是由mybatis进行实例化的,所以不能用spring工厂创建.
*也就不能用spring的注入语法. 所以需要开发一个工具类
*/
@Component
public class ApplicationContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtils.applicationContext = applicationContext;
}
//redisTemplate
public static Object getBean(String name){
return applicationContext.getBean(name);
}
}
/**
* 实现ibatis的cache
*/
@Slf4j
public class RedisCache implements Cache {
private String id;
public RedisCache(String id) {
log.info("当前的缓存id:[{}]",id);
this.id = id;
}
@Override
public String getId() {
return this.id;
}
@Override
public void putObject(Object key, Object value) { //放入redis缓存
log.info("放入缓存的key:[{}] , 放入缓存的value[{}]",key,value);
getRedisTemplate().opsForHash().put(id,key.toString(),value.toString());
}
@Override
public Object getObject(Object key) { //从redis缓存获取
log.info("获取缓存的key信息:[{}]",key.toString());
return getRedisTemplate().opsForHash().get(id,key);
}
@Override
public Object removeObject(Object key) { //删除指定缓存信息
return null;
}
@Override
public void clear() { //清除缓存
log.info("清除所有缓存...");
getRedisTemplate().delete(id);
}
@Override
public int getSize() {
return getRedisTemplate().opsForHash().size(id).intValue();
}
@Override
public ReadWriteLock getReadWriteLock() {
return null;
}
//封装获取redisTemplate
public RedisTemplate getRedisTemplate(){
RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
//redistemplate都是对象序列化,key想要直接使用字符串序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
在mapper文件中加入
<cache type="com.nmsl.Cache.RedisCache"/>
#效果
#缓存
2020-12-08 21:11:43.333 INFO 10764 --- [ main] com.nmsl.Cache.RedisCache : 当前的缓存id:[com.nmsl.dao.StudentDAO]
#第一次查询了数据库
2020-12-08 21:14:46.556 INFO 17096 --- [nio-8080-exec-2] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
2020-12-08 21:14:47.122 DEBUG 17096 --- [nio-8080-exec-2] com.nmsl.dao.StudentDAO.selectAll : ==> Preparing: select id,name,photopath,balance,age from t_student
2020-12-08 21:14:47.146 DEBUG 17096 --- [nio-8080-exec-2] com.nmsl.dao.StudentDAO.selectAll : ==> Parameters:
2020-12-08 21:14:47.223 DEBUG 17096 --- [nio-8080-exec-2] com.nmsl.dao.StudentDAO.selectAll : <== Total: 2
#之后从缓存中获取数据
2020-12-08 21:14:47.224 INFO 17096 --- [nio-8080-exec-2] com.nmsl.Cache.RedisCache : 放入缓存的key:[1711318415:-257010878:com.nmsl.dao.StudentDAO.selectAll:0:2147483647:select id,name,photopath,balance,age
from t_student:SqlSessionFactoryBean] , 放入缓存的value[[Student(id=5, name=罗汉伟, photopath=298255fa-3c22-41e9-85c8-a9c3a30a2198.gif, balance=15.0, age=43), Student(id=11, name=陈鑫, photopath=null, balance=23.0, age=25)]]
2020-12-08 21:15:31.139 INFO 17096 --- [nio-8080-exec-9] com.nmsl.Cache.RedisCache : 获取缓存的key信息:[1711318415:-257010878:com.nmsl.dao.StudentDAO.selectAll:0:2147483647:select id,name,photopath,balance,age
from t_student:SqlSessionFactoryBean]
2020-12-08 21:15:31.189 DEBUG 17096 --- [nio-8080-exec-9] com.nmsl.dao.StudentDAO : Cache Hit Ratio [com.nmsl.dao.StudentDAO]: 0.5