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 ...
随机推荐
- 【WebGL】1.WebGL简介
OpenGL和WebGL基本概念 OPEN GL:一个跨编程语言.跨平台的编程接口规格的专业的图形程序接口.它用于三维图像(二维的亦可),是一个功能强大,调用方便的底层图形库. WebGL:一个3D ...
- Easyui datagrid自定义排序
做项目遇到个关于排序问题,想着在前端排序,正好Easyui有这个功能,所以就拿来用了一下,因为跟官网的Demo不太一样,所以总结一下: 首先这一列是要排序的列(当然,在生产环境,这一列是隐藏的,在开发 ...
- 《Effective C++》第2章 构造/析构/赋值运算(2)-读书笔记
章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...
- 虚拟机下Redhat9 网络配置问题(转)
原文链接:http://www.programgo.com/article/38031929690/ edhat 9/redhat as 3装在虚拟机vmware上之后,连接网络是出现问题 Deter ...
- volatile关键字解析(一)
引起线程并发问题,可以简单的总结为以下三条: 原子性问题 可见性问题 有序性问题(重排序问题) 原子性问题 什么是原子性? 原子性,即一个操作或者多个操作,要么全部执行并且执行过程中不会被任何因素打断 ...
- windows下gvim搭建IDE
原文转载自:手把手教你把Vim改装成一个IDE编程环境(图文) 如侵犯您的版权,请联系:windeal12@qq.com By: 吴垠 Date: 2007-09-07 Version: 0.5 Em ...
- linux 下 iscsi的简单使用
一.准备工作 1.1.(服务端和客户端都需要设置) 系统使用的是CentOS6.8 禁用防火墙: 查看状态:# service iptables status 停止:# systemctl sto ...
- 微信小程序自定义tabbar的问题
个人感觉小程序的tab样式自定义的能力有所欠缺,不够美观,于是今天自己diy了一个tab 测试的时候发现,无论是使用navigator跳转(会出现点击的效果)还是用bindtap(触摸),因为没有定义 ...
- 我也说说Emacs吧(3) - 文件基本操作
Spacemacs文件基本操作 有了前两讲的积累,我们知道了: 1. 我们既要学习emacs的操作,也要学习vi的操作 2. emacs是一个可扩展的平台,我们并不是从头配置,而是使用一套成熟的集成方 ...
- Android中破解应用签名校验的后续问题处理方案(闪退和重启现象以及无效问题)
一.前言 之前已经写了一个爆破签名校验的工具kstools,很多同学也在使用,但是也反馈了不少问题,之前一篇文章也介绍了,关于爆破之后第三方登录问题修复,这篇我们在综合说明一下一些后遗症问题,关于ks ...