检索使用 spring redis 存储库存储的 POJO 的 TTL

Retrieve TTL of POJO stored using spring redis repository

我正在为我的项目使用 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;
}

上述 POJO 的存储库如下所示。

public interface ExampleRepository extends CrudRepository<Example, String> {

}

@TimeToLive 在将到期时间设置到 Redis 中时效果很好。在我的代码中,我尝试设置 timeToLive = 1200 的值。代码如下...

Example example = new Example();
example.setId("abc");
example.setTimeToLive(1200L);
exampleRepository.save(example);

当我尝试从 redis 检索 POJO 时,我的问题就开始了。

Example example = exampleRepository.findOne(id);
System.out.println(example.getTimeToLive());

以上代码始终打印 1200 作为输出。从 CLI,我可以确认 Redis 中存在的对象的 TTL 随着时间的推移而减少。然而,它没有设置为我要检索的 POJO。

为什么我无法检索此 POJO 实例在 redis 中设置的当前 ttl 值?

是否有使用 spring redis 存储库实现此目的的替代方法!

提前致谢!

目前不直接支持将生存时间值读回域对象。有DATAREDIS-523针对这个问题。

同时您可以通过 RedisTemplate.getExpire(key, timeUnit) 检索值,例如。 template.getExpire("Example:abc".getBytes(), TimeUnit.SECONDS).