RedisCacheConfig 文件

@Configuration
public class RedisCacheConfig {/*** 自定义的缓存key的生成策略 若想使用这个key* 只需要讲注解上keyGenerator的值设置为simpleKeyGenerator即可**/@Beanpublic KeyGenerator simpleKeyGenerator() {return (o, method, objects) -> {StringBuilder stringBuilder = new StringBuilder();stringBuilder.append(o.getClass().getSimpleName());stringBuilder.append(".");stringBuilder.append(method.getName());stringBuilder.append("[");for (Object obj : objects) {stringBuilder.append(obj.toString());}stringBuilder.append("]");return stringBuilder.toString();};}@Beanpublic CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),this.getRedisCacheConfigurationWithTtl(600), // 默认策略,未配置的 key 会使用这个this.getRedisCacheConfigurationMap() // 指定 key 策略);}private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();redisCacheConfigurationMap.put("UserInfoList", this.getRedisCacheConfigurationWithTtl(3000));redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000));return redisCacheConfigurationMap;}private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofSeconds(seconds));return redisCacheConfiguration;}
}

要指定 key 的过期时间,只要在getRedisCacheConfigurationMap方法中添加就可以。 然后只需要
@Cacheable 就可以把数据存入 redis

@Cacheable(value = "UserInfoList", keyGenerator = "simpleKeyGenerator") // 3000秒
@Cacheable(value = "UserInfoListAnother", keyGenerator = "simpleKeyGenerator") // 18000秒
@Cacheable(value = "DefaultKeyTest", keyGenerator = "simpleKeyGenerator") // 600秒,未指定的key,使用默认策略

补充:

根据个人情况选择使用哪种单位设置过期时间

.entryTtl(Duration.ofSeconds(seconds))设置时使用单位为秒
.entryTtl(Duration.ofMinutes(minutes));设置时使用单位为分钟
.entryTtl(Duration.ofHours(hours));设置时使用单位为小时

@Cacheable 设置缓存过期时间相关推荐

  1. SpringBoot @Cacheable Redis 设置缓存过期时间

    1.x 设置 @Bean @Primary public CacheManager cacheManager(RedisTemplate redisTemplate) {RedisCacheManag ...

  2. Nginx设置缓存过期时间

    Cache-Control location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|html)$ {#禁止缓存,每次都从服务器请求#add_header Cac ...

  3. 微信小程序设置缓存过期时间

    setCache(){if(this.judgeTime()){ //判断缓存是否过期,过期就重新添加一个wx.setStorageSync('oldTime',this.oldTime());} } ...

  4. 手写redis@Cacheable注解 支持过期时间设置

    原理解释 友情链接  手写redis @ Cacheable注解参数java对象作为键值 @Cacheable注解作用,将带有该注解方法的返回值存放到redis的的中; 使用方法在方法上使用@Cach ...

  5. spring boot 缓存redis设置定时过期时间

    前言 本篇文章分享的就是spring boot中的一个轮子,spring cache注解的方式实现接口数据缓存.默认的配置想非常简单,但是有一个弊端是缓存数据为永久缓存,本次将介绍如何设置接口缓存数据 ...

  6. Nginx设置静态页面压缩和缓存过期时间的方法 (转)

    使用nginx服务器的朋友可能都知道需要设置html静态页面缓存与页面压缩与过期时间的设置了,下面我来给各位同学介绍一下配置方法,包括对ico,gif,bmp,jpg,jpeg,swf,js,css, ...

  7. 微信小程序实现缓存过期时间

    微信小程序实现缓存过期时间 前言 一.设置缓存 二.得到缓存 总结 前言 先来看看官方文档 wx.setStorageSync(string key, any data) 将数据存储在本地缓存中指定的 ...

  8. Vue - 路由导航守卫控制访问权限,设置 localStorage 过期时间

    Vue 路由导航守卫控制访问权限,设置 localStorage 过期时间 一. 路由导航守卫 二. 路由导航守卫 简单使用 三. 使用路由导航守卫设置 token 过期时间 导航守卫: https: ...

  9. 微信小游戏服务器数据持久化,微信小程序wx.setStorage数据缓存实现缓存过期时间...

    为了项目性能等方面的考虑,有时候有必要为用户经常访问的页面使用缓存机制:在技术上,服务端和前端都有相应的缓存机制.比如传统的session及cookie等等,在微信小程序中,并没有cookie机制,但 ...

最新文章

  1. oracle dba_tables各字段含义
  2. ios 项目的.gitignore
  3. 你拿什么保护我的版权(写在中移动MM商用之前)
  4. 从硬盘上装xp手记(2005.8.14 )
  5. 巴克莱:对冲基金AI和大数据工作指南
  6. 你的专业 VS 你妈口中你的专业
  7. Paw 百度ai_直面落地!百度EasyDL产业智能创新大赛成果覆盖能源、交通、水利民生重业...
  8. mpythonx下载_【掌控】mpythonX:乐动盒子
  9. c语言学习宝典怎么样,C语言学习宝典
  10. idea教育版增加springboot插件
  11. 学习笔记-----usart串口调试助手一直打印00的解决方案
  12. winserver2012安装语言包
  13. Hadoop综合大作业
  14. 2022-7-6-18
  15. BZOJ#4816. [Sdoi2017]数字表格
  16. 信息安全审计系统S-Audit
  17. 干货!学霸用12个决策模型告诉你,如何判断你到底需不需要区块链!
  18. Python----第十次作业
  19. 福州大学计算机组成原理试题,2016年福州大学计算机专业基础D复试之计算机组成原理考研复试题库...
  20. Linux:系统性能监控工具-tsar安装和使用

热门文章

  1. [九度][何海涛] 字符串的排序
  2. 每日一道算法题:高楼扔鸡蛋问题(动态规划问题)
  3. 3.4.10nbsp;孙正义
  4. pythonmacd指标编写_Python版MACD指标画图范例
  5. win7计算机无用系统文件夹,c盘怎么清理到最干净(win7如何删除c盘无用文件)...
  6. 利用ptrace hook 系统调用
  7. Microsoft Edge浏览器打不开
  8. mysql拒绝访问root用户_对于出现拒绝访问root用户的解决方案
  9. Kotlin 概述【官方】
  10. C语言编程精粹读书笔记