如何防止redis中的缓存覆盖过期时间
How can I prevent cache override expiry time in redis
我有一个名为 save
的通用方法,使用 Redis 模板:
redisTemplate.expire(cacheType.name(), redisPropertyConfiguration.getTimeToLive(), TimeUnit.MINUTES);
每次我调用Redis模板的这个方法expire覆盖过期时间,我想防止过期时间并在过期时间结束时放置它
这是预期的,因为 expire(K key, long timeout, TimeUnit unit)
(wrapper of the EXPIRE Redis 命令)被记录为
Set time to live for given key.
你问:
I wanna prevent the expiration time and put it if the expiration time end
如果在过期时间之后检查密钥,则无法防止过期。
你可以做的是在已经过期的情况下再次添加密钥。
在 Redis 中,命令 TTL fooKey
returns 键的剩余生存时间。
好消息:Spring Boot Redis 模板 API 也实现了:
Get the time to live for key in seconds.
所以你可以这样写:
if (redisTemplate.getExpire(cacheType.name()) == -1L){
// re-add the key-value
redisTemplate.opsForValue.set(cacheType.name(), fooValue);
}
我有一个名为 save
的通用方法,使用 Redis 模板:
redisTemplate.expire(cacheType.name(), redisPropertyConfiguration.getTimeToLive(), TimeUnit.MINUTES);
每次我调用Redis模板的这个方法expire覆盖过期时间,我想防止过期时间并在过期时间结束时放置它
这是预期的,因为 expire(K key, long timeout, TimeUnit unit)
(wrapper of the EXPIRE Redis 命令)被记录为
Set time to live for given key.
你问:
I wanna prevent the expiration time and put it if the expiration time end
如果在过期时间之后检查密钥,则无法防止过期。
你可以做的是在已经过期的情况下再次添加密钥。
在 Redis 中,命令 TTL fooKey
returns 键的剩余生存时间。
好消息:Spring Boot Redis 模板 API 也实现了:
Get the time to live for key in seconds.
所以你可以这样写:
if (redisTemplate.getExpire(cacheType.name()) == -1L){
// re-add the key-value
redisTemplate.opsForValue.set(cacheType.name(), fooValue);
}