spring - 如何防止redis中的缓存覆盖到期时间
问题描述
我有一个save
用 Redis 模板调用的通用方法:
redisTemplate.expire(cacheType.name(), redisPropertyConfiguration.getTimeToLive(), TimeUnit.MINUTES);
每次我调用Redis模板的这个方法过期覆盖过期时间,我想阻止过期时间并在过期时间结束时放置它
解决方案
这是预期的,因为expire(K key, long timeout, TimeUnit unit)
(EXPIRE Redis 命令的包装器)被记录为
为给定的密钥设置生存时间。
你问 :
I wanna prevent the expiration time and put it if the expiration time end
如果您在过期时间之后检查密钥,则无法防止过期。
您可以做的是在已过期的情况下再次添加密钥。
在 Redis 中,该命令TTL fooKey
返回密钥的剩余生存时间。
好消息:Spring Boot Redis 模板 API 也实现了:
在几秒钟内获得关键的时间。
所以你可以写这样的东西:
if (redisTemplate.getExpire(cacheType.name()) == -1L){
// re-add the key-value
redisTemplate.opsForValue.set(cacheType.name(), fooValue);
}
推荐阅读
- openjfx - 如何将 openJFX11 画布保存为 png/位图?
- r - 根据分组数据评估的条件替换缺失值
- c# - C# Process.Start 无法连接到服务器
- java - MouseListener 没有给出准确的鼠标位置
- ios - UIview 顶部限制在安全区域,但在 viewDidLoad 中,视图的 Y 位置错误
- asp.net - 通过 SendGrid 向 Outlook 发送邮件总是失败
- c# - 向数据库添加新表后如何更新 Entity Framework Core
- c - 多线程进程的最大堆栈大小
- c++ - C++ 中的 C2276 或 C3867 编译器错误
- php - 这段代码有什么问题,总是打印结果 0。在 phpmyadmin 中直接运行查询时,我得到了正确的结果