配置redis缓存失效时间
2017-11-11 18:22
211 查看
最近想利用redis缓存做一个简陋版的类似腾讯新闻这样的查看新闻的网页。用了缓存以后,添加新闻缓存没有更新,想使用缓存的失效时间做到数据库缓存一致性。刚开始做的时候认为使用@CachePut注解会起到更新缓存的作用,设置了cacheName和key都和查找方法中的@Cacheable中的key和cacheName的一样,然而并没有成功,反而是被替换了,想想hashMap就能理解这个问题了。
如何设置,通过RedisCacheManage,看名字就知道应该通过它来设置
RedisCacheManage源码简单介绍
1.成员变量:
我们应该重点关注的是defaultExpiration和Map< String, Long> expires,只有这两个元素是和缓存失效时间有关的
2.构造函数
构造函数依次调用,第一个构造函数调用第二个构造函数,第二个构造函数调用第三个构造函数。我们在使用的时候都是用的第一个构造函数,传递进一个redisTemplate。在第三个构造函数中defaultExpiration = 0L,过期时间为0,表示缓存用不失效,expires = null表示没有为任何缓存设置缓存失效时间
3.设置缓存失效的方法
注意:传入的参数为Map类型的
如果传入的参数不为null。则将该值传递给expires,并且通过ConcurrentHashMap(expires)包装了一下,保证是线程安全的。
4.用到expires参数的方法
protected long computeExpiration(String name) {
Long expiration = null;
if(this.expires != null) {
expiration = (Long)this.expires.get(name);
}
通过get方法获得对应缓存区域的缓存失效时间。如果没有设置缓存失效时间,则默认永远不失效
5.配置缓存失效时间
在配置redis的配置文件中进行修改,我用的是java配置文件
时间是以秒作为单位的,Map中对应的键值对为缓存名和对应的缓存失效时间
如何设置,通过RedisCacheManage,看名字就知道应该通过它来设置
RedisCacheManage源码简单介绍
1.成员变量:
private final Log logger; private final RedisOperations redisOperations; private boolean usePrefix; private RedisCachePrefix cachePrefix; private boolean loadRemoteCachesOnStartup; private boolean dynamic; private long defaultExpiration; private Map<String, Long> expires; private Set<String> configuredCacheNames; private final boolean cacheNullValues;
我们应该重点关注的是defaultExpiration和Map< String, Long> expires,只有这两个元素是和缓存失效时间有关的
2.构造函数
public RedisCacheManager(RedisOperations redisOperations) { this(redisOperations, Collections.emptyList()); } public RedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames) { this(redisOperations, cacheNames, false); } public RedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames, boolean cacheNullValues) { this.logger = LogFactory.getLog(RedisCacheManager.class); this.usePrefix = false; this.cachePrefix = new DefaultRedisCachePrefix(); this.loadRemoteCachesOnStartup = false; this.dynamic = true; this.defaultExpiration = 0L; this.expires = null; this.redisOperations = redisOperations; this.cacheNullValues = cacheNullValues; this.setCacheNames(cacheNames); }
构造函数依次调用,第一个构造函数调用第二个构造函数,第二个构造函数调用第三个构造函数。我们在使用的时候都是用的第一个构造函数,传递进一个redisTemplate。在第三个构造函数中defaultExpiration = 0L,过期时间为0,表示缓存用不失效,expires = null表示没有为任何缓存设置缓存失效时间
3.设置缓存失效的方法
public void setExpires(Map<String, Long> expires) { this.expires = expires != null?new ConcurrentHashMap(expires):null; }
注意:传入的参数为Map类型的
如果传入的参数不为null。则将该值传递给expires,并且通过ConcurrentHashMap(expires)包装了一下,保证是线程安全的。
4.用到expires参数的方法
protected long computeExpiration(String name) {
Long expiration = null;
if(this.expires != null) {
expiration = (Long)this.expires.get(name);
}
return expiration != null?expiration.longValue():this.defaultExpiration; }
通过get方法获得对应缓存区域的缓存失效时间。如果没有设置缓存失效时间,则默认永远不失效
5.配置缓存失效时间
在配置redis的配置文件中进行修改,我用的是java配置文件
/** * redis缓存管理器 * */ @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); Map<String, Long> expires = new HashMap<String, Long>(); expires.put("news", 60L); redisCacheManager.setExpires(expires); return redisCacheManager; }
时间是以秒作为单位的,Map中对应的键值对为缓存名和对应的缓存失效时间
相关文章推荐
- Spring Boot缓存实战 Redis 设置有效时间和自动刷新缓存,时间支持在配置文件中配置
- Spring 定时任务(精)(session工具类(配置失效时间),cookie工具类 web工程(初始化容器) redis工具类)
- redis+spring注解方式实现配置缓存时间过期
- Spring Boot Cache + redis 设置有效时间和自动刷新缓存,时间支持在配置文件中配置
- redis结合springboot设置不同缓存失效配置
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- Redis配置参数的热修改 设置有效时间
- redis和memcache缓存击穿,缓存失效问题
- redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置
- Memcache关于缓存失效时间的问题
- Redis的缓存策略和主键失效机制
- redis做缓存手动更新,redis和gearmand实现自动更新,redis高可用配置
- redis 失效时间
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- Redis 作为缓存服务器的配置
- 配置Redis作为缓存(六种淘汰策略)
- Laravel 下配置缓存、Session 使用不同的 Redis 数据库
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- redis 仅用做缓存的配置