springboot中使用cache和redis

知識點:springboot中使用cache和redishtml

 (1)springboot中,整合了cache,咱們只須要,在入口類上加 @EnableCaching 便可開啓緩存redis

 例如:在service層使用@Cacheable和CacheEvictspring

 

//添加緩存
@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亂碼 可參考另外一篇博客解決辦法 https://www.cnblogs.com/shuaifing/p/11213253.html以後會總結1.cache其餘用法 2.springboot中的原理 參考:https://blog.csdn.net/weixin_36279318/article/details/82820880
相關文章
相關標籤/搜索