springboot中使用cache和redis

知识点: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


 



 

猜你喜欢

转载自www.cnblogs.com/shuaifing/p/11051651.html