Pra*_*aju 4 java spring-data-redis spring-repositories
我在我的项目中使用 Spring redis 存储库来将 POJO 持久保存到 redis 中。以下示例详细说明了我想要实现的目标。
@Data
@RedisHash("Example")
public class Example {
@Id
private String id;
private String attributeA;
private String attirbuteB;
@TimeToLive(unit = TimeUnit.SECONDS)
private Long timeToLive;
}
Run Code Online (Sandbox Code Playgroud)
上述 POJO 的存储库如下所示。
public interface ExampleRepository extends CrudRepository<Example, String> {
}
Run Code Online (Sandbox Code Playgroud)
@TimeToLive 在将过期时间设置到 redis 中效果很好。在我的代码中,我尝试设置 timeToLive = 1200 的值。代码如下......
Example example = new Example();
example.setId("abc");
example.setTimeToLive(1200L);
exampleRepository.save(example);
Run Code Online (Sandbox Code Playgroud)
当我尝试从 Redis 检索 POJO 时,我的问题就开始了。
Example example = exampleRepository.findOne(id);
System.out.println(example.getTimeToLive());
Run Code Online (Sandbox Code Playgroud)
上面的代码始终打印 1200 作为输出。从 CLI 中我可以确认 Redis 中存在的对象的 TTL 随着时间的推移而减少。然而,它没有设置为我试图检索的 POJO。
为什么我无法检索此 POJO 实例的 redis 中设置的 ttl 当前值?
有没有其他方法可以使用 spring redis 存储库来实现此目的!
提前致谢!
当前不直接支持将生存时间值读回到域对象中。DATAREDIS-523就是针对这个问题的。
同时您可以通过例如检索该值RedisTemplate.getExpire(key, timeUnit)。template.getExpire("Example:abc".getBytes(), TimeUnit.SECONDS)。