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 ...
随机推荐
- is null 和=null的区别
数据库中 null 表示 不可知,不确定 所以 判断都用 字段 is null的方式进行判断 而 = null .<> null 的判断结果,仍然是不可知,不确定,所以 不会返回任何结果. ...
- PostgreSQL时间格式及相关函数实践
在创建表的时候,有客户需要将时间转为字符串,而且要求了具体的格式,如:20181115101010001.方便记录数据的更新时间,貌似是给Mysql使用,当时就很蛋疼,时间格式存储子啊数据库中就是va ...
- c# DataTable导出为excel
/// <summary> /// 将DataTable导出为Excel文件(.xls) /// </summary> /// <param name="dt& ...
- SDP协议简述
SDP协议也是文本协议,只需要按照协议本身的格式填充.SDP协议格式即详细信息如下: 会话描述 格式及举例 v=(protocol version) v=0 o=(owner/creator and ...
- wma wmv asf格式分析
原文链接:http://blog.csdn.net/werocpp/article/details/5594067 原文链接:http://blog.chinaunix.net/uid-2075819 ...
- [Scala]Scala学习笔记五 Object
1. 单例对象 Scala没有静态方法或静态字段,可以使用object来达到这个目的,对象定义了某个类的单个实例: object Account{ private var lastNumber = 0 ...
- 每周荐书:Kotlin、分布式、Keras(评论送书)
每周荐书:Kotlin.分布式.Keras(评论送书) 感谢大家对每周荐书栏目的支持,先公布下上周中奖名单 法式三文鱼 名优秀评论可以免费获得此书. Kotlin实战 首著席卷而来 Android ...
- Java调用本地方法又是怎么一回事
JNI JNI即Java Native Interface,它能在Java层实现对本地方法的调用,一般本地的实现语言主要是C/C++,其实从虚拟机层面来看JNI挺好理解,JVM主要使用C/C++ 和少 ...
- Python IDE in Sublime
(最近换了电脑,然后忘了把 ST 的配置搬过来,所以重新折腾了一遍 Sublime 中的 Python 环境配置) 以下插件均通过 Package Control 安装. SublimeREPL 快捷 ...
- SQl_update,case_when,end
627.Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and ...