据javadoc报道,
expire
是
Set time to live for given key.
.此外,你的
@Bean public RedisTemplate<String, Object> redisTemplate(...)
将被称为
当Spring应用程序启动时
,
只有一次
.
因此,您编写的代码的意思是,“当spring启动时,使该密钥过期。但我不关心spring启动后的任何事情。”这与您想要做的不同——正如您所提到的“我试图在每个缓存上设置expire”。
如果需要“每次保存缓存时设置过期”,那么在保存时更改命令如何。而不是使用
SET
在redis中,您应该使用
SETEX
(见
https://redis.io/commands/setex
)例如,春天的对应物是,
void set(K key, V value, long timeout, TimeUnit unit)
(
https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/ValueOperations.html
).