SpringBoot @Cacheable Redis 设置缓存过期时间
1.x 设置
@Bean
@Primary
public CacheManager cacheManager(RedisTemplate redisTemplate) {RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);Map<String, Long> expires = new HashMap<>();expires.put("timeout", 60L);// 设置超时
// 根据特定名称设置有效时间redisCacheManager.setExpires(expires);// 设置默认的时间redisCacheManager.setDefaultExpiration(cacheDefaultExpiration);return redisCacheManager;}
使用方式:
转载:传送门
@Configuration
//@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600 * 12)//最大过期时间
@EnableCaching
public class RedisConfig {@Beanpublic CacheManager cacheManager(RedisTemplate redisTemplate) {RedisCacheManager rcm = new RedisCacheManager(redisTemplate);//设置缓存过期时间Map<String, Long> expires = new HashMap<>();expires.put("12h", 3600 * 12L);expires.put("1h", 3600 * 1L);expires.put("10m", 60 * 10L);rcm.setExpires(expires);
// rcm.setDefaultExpiration(60 * 60 * 12);//默认过期时间return rcm;}
}//----------------------------------------------------------@Cacheable(value = "12h", key = "#root.methodName")@Overridepublic List<User> getUserArticleRank() {//获得排行榜前10名的用户,每12小时刷新一次return userRepository.findTop10ByArticleSize();}
2.x 设置
转载:传送门
/*** 2.XX版本的配置**/@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); // 生成一个默认配置,通过config对象即可对缓存进行自定义配置config = config.entryTtl(Duration.ofMinutes(2)) // 设置缓存的默认过期时间,也是使用Duration设置.disableCachingNullValues(); // 不缓存空值// 设置一个初始化的缓存空间set集合Set<String> cacheNames = new HashSet<>();cacheNames.add("catalog_test_id");cacheNames.add("catalog_test_name");// 对每个缓存空间应用不同的配置Map<String, RedisCacheConfiguration> configMap = new HashMap<>();configMap.put("catalog_test_id", config);configMap.put("catalog_test_name", config.entryTtl(Duration.ofMinutes(5)));RedisCacheManager cacheManager = RedisCacheManager.builder(factory) // 使用自定义的缓存配置初始化一个cacheManager.initialCacheNames(cacheNames) // 注意这两句的调用顺序,一定要先调用该方法设置初始化的缓存名,再初始化相关的配置.withInitialCacheConfigurations(configMap).build();return cacheManager;}
@CacheConfig(cacheNames = "catalog_test_name")
public class SsoCache{@Cacheable(keyGenerator = "wiselyKeyGenerator")public String getTokenByGsid(String gsid)
}-------------------------------------
自定义Redis Cache 自动化配置替换系统默认的cacheManager* 扩展cache name 支持 # 号分隔 cache name 和 超时 ttl(单位秒)。<p>* 示例:@CachePut(value = "user#300", key = "#id")* </p>参考:https://gitee.com/brucewuu/spring-ultron/blob/master/spring-ultron-projects/ultron-redis/src/main/java/org/springultron/redis/config/RedisCacheAutoConfiguration.java---------------------------------------------------------
使用(name中增加“#”,后面是过期时间,不加则走默认时间)@Cacheable(cacheNames = "catalog_test_name#120", unless = "#result==null")public UserEntity findUserByUserName(String userName) {return userRepository.findUserByUserName(userName);}
SpringBoot @Cacheable Redis 设置缓存过期时间相关推荐
- spring boot 缓存redis设置定时过期时间
前言 本篇文章分享的就是spring boot中的一个轮子,spring cache注解的方式实现接口数据缓存.默认的配置想非常简单,但是有一个弊端是缓存数据为永久缓存,本次将介绍如何设置接口缓存数据 ...
- Nginx设置缓存过期时间
Cache-Control location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|html)$ {#禁止缓存,每次都从服务器请求#add_header Cac ...
- @Cacheable 设置缓存过期时间
RedisCacheConfig 文件 @Configuration public class RedisCacheConfig {/*** 自定义的缓存key的生成策略 若想使用这个key* 只需要 ...
- php中redis设置队列过期时间,如何在php中使用redis队列操作
如何在php中使用redis队列操作 发布时间:2021-02-04 18:36:00 来源:亿速云 阅读:90 作者:Leah 这篇文章给大家介绍如何在php中使用redis队列操作,内容非常详细, ...
- 微信小程序设置缓存过期时间
setCache(){if(this.judgeTime()){ //判断缓存是否过期,过期就重新添加一个wx.setStorageSync('oldTime',this.oldTime());} } ...
- 在Redis中设置了过期时间的Key,需要注意哪些问题?
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:千山qianshan juejin.im/post/5d6b ...
- 设置过期时间_在Redis中设置了过期时间的Key,需要注意哪些问题?
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:千山qianshan juejin.im/post/5d6b ...
- 在 Redis 中设置了过期时间的 Key,需要注意哪些问题?
熟悉Redis的同学应该知道,Redis的每个Key都可以设置一个过期时间,当达到过期时间的时候,这个key就会被自动删除. 在为key设置过期时间需要注意的事项 1. DEL/SET/GETSET等 ...
- 【干货】在Redis中设置了过期时间的Key怎么办?
熟悉Redis的同学应该知道,Redis的每个Key都可以设置一个过期时间,当达到过期时间的时候,这个key就会被自动删除. 在为key设置过期时间需要注意的事项 1. DEL/SET/GETSET等 ...
最新文章
- c 远程编辑linux文件,makefile - 在远程Linux机器上编译C ++ - “检测到时钟偏差”警告...
- Java中栈、堆和常量池
- python redis处理高并发-Python高并发解决方案实现过程详解
- 网易云信获中国IM云服务领域首个CSA STAR认证
- Typeface 字体样式
- LOJ2195 旅行
- 一位跟我一样疑惑的同学~
- 距离,原来还有这么多类
- OpenCV和java做人脸识别
- IIS6配置和PHP5.6环境安装
- [Mac OS] Mac OS X for x86 下载
- ftp误删服务器文件恢复,ftp服务器文件误删
- 技能分享 | 麦肯锡教给我的写作武器:如何讲好一句话
- 中英文说明书丨艾美捷HEK293T宿主细胞蛋白ELISA试剂盒
- 家用智能洗地机哪个牌子好、洗地机品牌排行榜前十名介绍
- Li‘s 核磁共振影像数据处理-23-itk-snap调整影像方向功能介绍
- 带参宏定义和带参函数的区别
- BugKu_python_jail
- 人工智能之父,你知道是谁吗?
- MySQL:Got error 139 from storage engine