首页 > 编程语言 > 详细

springboot整合redis缓存

时间:2019-04-01 16:32:02      阅读:119      评论:0      收藏:0      [点我收藏+]

技术分享图片

  使用springBoot添加redis缓存需要在POM文件里引入

  org.springframework.bootspring-boot-starter-cacheorg.springframework.bootspring-boot-starter-redis1.4.3.RELEASE

  我们添加缓存的支持需要两个依赖,一个是SpringBoot内部的缓存配置、另外则是我们的redis缓存。

  配置Redis数据库

  依赖添加完成后,需要配置我们本地的redis数据库连接到项目中,我们打开application-local.properties配置文件添加如下图8所示的配置内容:

  #redisspring.redis.cluster.nodes=172.0.0.1:6379spring.redis.pool.max-active=20spring.redis.pool.max-idle=10spring.redis.pool.min-idle=5spring.redis.pool.max-wait=10spring.redis.timeout=5000

  1、简易方式

  @Configuration @EnableCachingpublic class RedisConfig extends CachingConfigurerSupport {//生成key @Override@Beanpublic KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... objects) { return UserConstant.REDIS_KEY; //常量key } };}

  }

  查看一下 Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用参考

  (https://www.cnblogs.com/fashflying/p/6908028.html )

  // serviceImpl 方法 @Cacheable(infos)

  @Override@Cacheable(infos) public ListqueryUser() { return this.UserMapper.queryUser();}

  如上只需要加上一个注解就可以 在调用查询的时候先去缓存里面找,没有就执行方法 然后再存到缓存里面

  在执行增、删、改的时候需要删除缓存如下:

  @Override@CacheEvict(infos) public void editUserStatus(Mapinfo) { UserMapper.editStatus(info);}

  在对应的方法上加入注解 这样就会删除缓存

  最后去测试一下就可以。

?

 

springboot整合redis缓存

原文:https://www.cnblogs.com/qfjavabd/p/10636801.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!