首页 > 编程语言 > 详细

springboot中使用cache和redis

时间:2019-06-19 16:33:56      阅读:157      评论:0      收藏:0      [点我收藏+]

知识点:springboot中使用cache和redis

 (1)springboot中,整合了cache,我们只需要,在入口类上加 @EnableCaching 即可开启缓存

 例如:在service层使用@Cacheable和CacheEvict

 

 //添加缓存
@Cacheable(cacheNames = "TestCACHE",key = "#root.methodName +‘_‘+ #id")
public Map<String, Object> testSetCache(Integer id){
Map<String, Object> user = userMapper.findUserById(id);
return user;
}

//清除缓存
@CacheEvict(cacheNames = "TestCACHE", allEntries = true)
public Boolean testEvictCache(){
return true;
}

(2)引入redis依赖,将Cache中的数据放到redis数据库中,然后从redis中取数据

a.引入依赖
<!--加入redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 b.application.yml中设置redis连接等配置

redis:
host: 192.10.21.237
port: 6379
database: 5
password:
timeout: 1800000
jedis:
pool:
max-idle: 10
min-idle: 0
max-active: 10
max-wait: 1000

存入redis的数据如下
技术分享图片






问题:key乱码
之后会总结1.cache其他用法 2.springboot中的原理

参考:https://blog.csdn.net/weixin_36279318/article/details/82820880


 



 

 

springboot中使用cache和redis

原文:https://www.cnblogs.com/shuaifing/p/11051651.html

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