springboot实现mysql数据的Redis缓存

本文介绍了如何在SpringBoot项目中使用Redis作为缓存,将MySQL数据库中的数据进行缓存操作,提高应用性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
*因为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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值