首页 文章

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

提问于
浏览
0

我正在为我的项目使用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存储库实现此目的的替代方法!

提前致谢!

1 回答

  • 0

    目前不直接支持将时间值读回域对象 . DATAREDIS-523针对此问题 .

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

相关问题