Spring Caching配置缓存过期时间
文章目录
- 一、Spring Cache是什么?
- 二、使用步骤
- 1.开启基于注解的缓存
- 2.配置缓存
- 三、解决方案
- 方案一:通过编写config设置缓存相关项
- 方案二:通过配置文件
一、Spring Cache是什么?
它利用了AOP,实现了基于注解的缓存功能,并且进行了合理的抽象,业务代码不用关心底层是使用了什么缓存框架,只需要简单地加一个注解,就能实现缓存功能了。而且Spring Cache也提供了很多默认的配置,用户可以3秒钟就使用上一个很不错的缓存功能。
工作流程:
- 使用Spring Cache分为很简单的三步:添加依赖(springboot依赖包内置),开启缓存,加缓存注解。
- 每次调用该方法时,都会检查缓存以查看调用是否已经运行并且不必重复。虽然在大多数情况下,只声明了一个缓存,但注释允许指定多个名称,以便使用多个缓存。在这种情况下,在调用该方法之前检查每个缓存 - 如果至少命中一个缓存,则返回关联的值。
- 会触发一个后置处理,这会扫描每一个spring bean,查看是否已经存在缓存。如果找到了,就会自动创建一个代理拦截方法调用,使用缓存的bean执行处理。
特性:
缓存数据是存在redis
默认永不过期
key-value键、值队存储
二、使用步骤
1.开启基于注解的缓存
- 在启动类添加以下注解
@EnableCaching
2.配置缓存
- 在需要缓存数据的方法上面添加
@Cacheable
注解,即可缓存这个方法的返回值。
@Cacheable(value = "DefaultKeyTest", keyGenerator = "simpleKeyGenerator", /*cacheNames = API_DETAIL_KEY_PREFIX, key = "target.redisApiDetailKeyPrefix + ':' + #appCode",*/ unless = "#result == null")public OpenApiDetailVo findByAppCode(String appCode) {return openApiAuthService.queryDetail(appCode);}
三、解决方案
方案一:通过编写config设置缓存相关项
- 要指定 key 的过期时间,只需要getRedisCacheConfigurationMap方法中添加就可以。
@Configuration
public class RedisCacheConfig {@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.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.WRAPPER_ARRAY);jackson2JsonRedisSerializer.setObjectMapper(om);RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofSeconds(seconds));return redisCacheConfiguration;}
}
- 下面给出三种案例
// 3000秒@Cacheable(value = "UserInfoList", keyGenerator = "simpleKeyGenerator")// 18000秒@Cacheable(value = "UserInfoListAnother", keyGenerator = "simpleKeyGenerator")// 600秒,未指定的key,使用默认策略@Cacheable(value = "DefaultKeyTest", keyGenerator = "simpleKeyGenerator")
方案二:通过配置文件
spring:# maximumSize:配置缓存的最大条数,当快要达到容量上限的时候,缓存管理器会根据一定的策略将部分缓存项移除。# expireAfterAccess:配置缓存项的过期机制,缓存项固定30秒将会过期,从而被移除。cache:caffeine:spec: maximumSize=500, expireAfterAccess=30stype: caffeine
Spring Caching配置缓存过期时间相关推荐
- Nginx设置静态页面压缩和缓存过期时间的方法 (转)
使用nginx服务器的朋友可能都知道需要设置html静态页面缓存与页面压缩与过期时间的设置了,下面我来给各位同学介绍一下配置方法,包括对ico,gif,bmp,jpg,jpeg,swf,js,css, ...
- Nginx设置缓存过期时间
Cache-Control location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|html)$ {#禁止缓存,每次都从服务器请求#add_header Cac ...
- SpringBoot @Cacheable Redis 设置缓存过期时间
1.x 设置 @Bean @Primary public CacheManager cacheManager(RedisTemplate redisTemplate) {RedisCacheManag ...
- 设置日志不记录指定类型的文件,日志文件的切割,配置静态元素过期时间
访问日志不记录指定类型的文件 修改虚拟主机配置文件 先不重新加载配置,测试访问jpg文件,是否记录日志 测试结果是还是记录了jpg文件的日志 -t graceful重新加载一次,就会发现不会记录jpg ...
- 微信小程序实现缓存过期时间
微信小程序实现缓存过期时间 前言 一.设置缓存 二.得到缓存 总结 前言 先来看看官方文档 wx.setStorageSync(string key, any data) 将数据存储在本地缓存中指定的 ...
- 微信小游戏服务器数据持久化,微信小程序wx.setStorage数据缓存实现缓存过期时间...
为了项目性能等方面的考虑,有时候有必要为用户经常访问的页面使用缓存机制:在技术上,服务端和前端都有相应的缓存机制.比如传统的session及cookie等等,在微信小程序中,并没有cookie机制,但 ...
- @Cacheable 设置缓存过期时间
RedisCacheConfig 文件 @Configuration public class RedisCacheConfig {/*** 自定义的缓存key的生成策略 若想使用这个key* 只需要 ...
- spring cloud设置session过期时间
一.错误步骤特别说明 首先说明一下spring cloud gateway,修改下面是无效的(无论是改成3600,1m等等) server: servlet: session: ...
- 【微信小程序】缓存过期时间的相关设置
微信小程序缓存机制介绍 每个微信小程序都可以有自己的本地缓存,可以通过 wx.setStorage(wx.setStorageSync).wx.getStorage(wx.getStorageSy ...
最新文章
- 超级计算机的生产流程,精密陶瓷的生产流程分为几个步骤(图)
- linux安装通用plsql数据库,linux centOs中安装好数据库,客户端用plsql连接oracle
- Maximum Subrectangle(矩阵,前缀和)
- 程序闪退_苹果APP日常使用时一直闪退怎么办?
- 通用日志系统开发【转】
- React Redux: 从文档看源码 - Components篇
- python自带sqlite_python内置的sqlite3模块,使用其内置数据库
- 非基元类型数据结构_Java数据类型–基元和二进制文字
- Android不同版本下Notification创建方法
- 课程设计matlab仿真,MATLAB与仿真系统课程设计报告
- 顶岗实习周记计算机专业40篇,计算机顶岗实习周记20篇
- C讲坛之猜数字游戏的实现
- 气象台发布的拼图重投影示例
- 漏洞平台之pikachu详细图文搭建教程
- mysql 错误 #2054 解决方案
- 今天两位80后程序员身价超千亿 一大波快手员工财富自由
- 计算机桌面结构,桌面主机的内部结构桌面主机的组成是什么[详细介绍]
- 安卓高通Diag命令解析
- 网吧母盘制作其本流程
- 什么样的人才培养才能在企业中发挥重要作用?