spring cache之自定义keys的过期时间
spring @cacheable注解默认不支持方法级别的缓存失效时间,只能通过配置来配置全局的失效时间
如果需要实现对方法级别的缓存支持失效时间机制,有一种比较简单的方法,spring配置文件如下:
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
- <context:component-scan base-package="redis.cache"/>
- <context:annotation-config/>
- <cache:annotation-driven cache-manager="redisCacheManager"/>
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:redis.properties</value>
- </list>
- </property>
- </bean>
- <!-- 配置JedisPoolConfig实例 -->
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxIdle" value="${redis.maxIdle}"/>
- <property name="maxTotal" value="${redis.maxActive}"/>
- <property name="maxWaitMillis" value="${redis.maxWait}"/>
- <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
- </bean>
- <!-- 配置JedisConnectionFactory -->
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="hostName" value="${redis.host}"/>
- <property name="port" value="${redis.port}"/>
- <property name="password" value="${redis.pass}"/>
- <property name="database" value="${redis.dbIndex}"/>
- <property name="poolConfig" ref="poolConfig"/>
- </bean>
- <!-- 配置RedisTemplate -->
- <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
- <property name="connectionFactory" ref="jedisConnectionFactory"/>
- </bean>
- <!-- 配置RedisCacheManager -->
- <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
- <constructor-arg name="redisOperations" ref="redisTemplate"/>
- <property name="defaultExpiration" value="${redis.expiration}"/>
- <property name="expires">
- <map>
- <entry key="test" value="30"/>
- </map>
- </property>
- </bean>
- </beans>
配置文件中的redisCacheManager对象配置了expires属性,该属性是一个map,可以用来设置某些keys的过期时间
defaultExpiration属性设置了全局的默认失效时间,而expires属性则根据map指定的key单独设置失效时间,可以指定多对key value,
时间单位为:秒
注意:expires属性中map的key对应的是@cacheable注解中的value属性指定的值,而不是key。例如:
- @Cacheable(value = "test", key = "'testUser_' + #id")
- public String testExpire(Long id) {
- return "test";
- }
如果要为上述方法设置过期时间30秒,则配置文件中expires属性的key值一定要指定test,而不是testUser_#id
spring cache之自定义keys的过期时间的更多相关文章
- spring-redis-session 自定义 key 和过期时间
对于分布式应用来说,最开始遇到的问题就是 session 的存储了,解决方案大致有如下几种 使用 spring-session 它可以把 session 存储到你想存储的位置,如 redis,mysq ...
- Spring Cache缓存框架
一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...
- 「性能提升」扩展 Spring Cache 支持多级缓存
为什么多级缓存 缓存的引入是现在大部分系统所必须考虑的 redis 作为常用中间件,虽然我们一般业务系统(毕竟业务量有限)不会遇到如下图 在随着 data-size 的增大和数据结构的复杂的造成性能下 ...
- Redis 过期时间
http://www.redis.cn/commands/expire.html 附录: Redis 过期时间 Keys的过期时间 通常Redis keys创建时没有设置相关过期时间.他们会一直存在, ...
- java实现带过期时间的缓存
private static ScheduledExecutorService swapExpiredPool = new ScheduledThreadPoolExecutor(10); priva ...
- springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间
springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...
- SpringCache自定义过期时间及自动刷新
背景前提 阅读说明(十分重要) 对于Cache和SpringCache原理不太清楚的朋友,可以看我之前写的文章:Springboot中的缓存Cache和CacheManager原理介绍 能关注Spri ...
- C# Cache 设定缓存过期时间方法 绝对过期时间 和 相对过期时间(即:访问激活后不过期)
摘自: http://www.cnblogs.com/zj1111184556/p/3493840.html 1. 设定绝对过期时间 /// <summary> /// 设定绝对的过期时间 ...
- HttpContext.Current.Cache 过期时间
原文:HttpContext.Current.Cache 过期时间 为了更快的读取数据,我们一般会把常用到的数据加载到Cache中 在.NET中,Cache的存在可以依赖多中方式,主要用到HttpCo ...
随机推荐
- SQL Plus常用命令
1.常用命令:显示当前用户名:show user登录到数据库:conn[etc] 用户名/密码@网络[as sysdba / as sysoper] 简单写法:conn 用户名/密码 con ...
- 使用Apache的ab工具进行网站性能测试
Apache服务器自带了ab压力测试工具,可以用来测试网站性能,使用简单方便. ab 的用法是:ab [options] [http://]hostname[:port]/path 例如:ab -n ...
- igmpproxy源代码学习——igmpProxyInit()
igmpproxy源代码学习--igmpProxyInit()函数详解,igmpproxy初始化 在运行igmpproxy的主程序igmpproxyRun()之前需要对igmpproxy进行一些配置, ...
- 一个功能丰富的 jQuery 树形插件 z-tree
链接 如果你的树 很复杂, 需要拖拽功能, 还可以考虑用这个 另外还有一个目前在用 Dynatree 如果一般的树, 还是自己写一个, 也很轻松, 如果有一两个复杂的点, 可以参考ZTree
- 《Effective Java》读书笔记(二)之对于所有对象都通用的方法
第八条 在改写equals的时候请遵守通用约定 一般以下几种情况,不适宜覆盖equals方法 1.类的每个实例本质上都是唯一的,对于代表活动实体而不是值的类确实如此,例如Thread. 2.不关心类是 ...
- Java调用本地方法又是怎么一回事
JNI JNI即Java Native Interface,它能在Java层实现对本地方法的调用,一般本地的实现语言主要是C/C++,其实从虚拟机层面来看JNI挺好理解,JVM主要使用C/C++ 和少 ...
- C#中PadLeft和PadRight小知识点
当我们显示字符串数据时,有时候我们需要考虑数据的排列美观. 比如一些人名和一些编号,我们想让他们整齐对齐显示等. C# String类提供了2种操作方法: String.PadLeft(int tot ...
- 细说C语言的优先级和结合性
Table0. 为什么要掌握优先级1. 优先级1.1 优先级图表1.2 运算符实例1.3 优先级顺口溜2. 结合性3. 参考资料 写代码的时候,常会翻看的一个表就是“c语言运算符优先级表”.c的运算符 ...
- UVALive - 4108 SKYLINE (吉司机线段树)
题目链接 题意:在一条直线上依次建造n座建筑物,每座建筑物建造完成后询问它在多长的部分是最高的. 比较好想的方法是用线段树分别维护每个区间的最小值mi和最大值mx,当建造一座高度为x的建筑物时,若mi ...
- 向requestAnimationFrame的回调函数中传递参数
其实跟setTimeout类似,我们知道传参传的是一个函数,那么我们是不是可以用一个匿名函数来包裹这个函数的执行呢function fn(fc){ console.log('fc:',fc) fc++ ...