java RedisTemplate 过期不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30432479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RedisTemplate expire doesn't work
提问by Nikolas
I'm trying to test expire method in RedisTemplate. For example, I store session in redis, and than try to retrieve session and check that values are the same. For expire session I use expire() method of redisTemplate and for getting expired session I use getExpire() method. But it doesn't work. How can I test value, that stored in redis?
我正在尝试在 RedisTemplate 中测试 expire 方法。例如,我将 session 存储在 redis 中,然后尝试检索 session 并检查值是否相同。对于过期会话,我使用 redisTemplate 的 expire() 方法,对于获取过期会话,我使用 getExpire() 方法。但它不起作用。如何测试存储在 redis 中的值?
//without import and fields
public class Cache() {
private StringRedisTemplate redisTemplate;
public boolean expireSession(String session, int duration) {
return redisTemplate.expire(session, duration, TimeUnit.MINUTES);
}
}
//Test class without imports and fields
public class TestCache() {
private Cache cache = new Cache();
@Test
public void testExpireSession() {
Integer duration = 16;
String session = "SESSION_123";
cache.expireSession(session, duration);
assertEquals(redisTemplate.getExpire(session, TimeUnit.MINUTES), Long.valueOf(duration));
}
}
but test fails with AssertionError:
但测试失败并出现 AssertionError:
Expected :16 Actual :0
预期:16 实际:0
UPDATE:I thought, that getExpire() method doesn't work, but in fact expire() method doesn't work. It returns false. redisTemplate is a spring Bean that autowired to test class. There are many other test methods in TestCache class that work correctly.
更新:我认为 getExpire() 方法不起作用,但实际上 expire() 方法不起作用。它返回假。redisTemplate 是一个自动装配到测试类的 spring Bean。TestCache 类中有许多其他测试方法可以正常工作。
回答by mp911de
I set up following code to perform a test on getExpire()
(jedis 2.5.2, spring-data-redis 1.4.2.RELEASE):
我设置了以下代码来执行测试getExpire()
(jedis 2.5.2,spring-data-redis 1.4.2.RELEASE):
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
public class DemoApplicationTests {
@Autowired
private RedisTemplate<String, String> template;
@Test
public void contextLoads() {
template.getConnectionFactory().getConnection().flushAll();
assertFalse(template.hasKey("key"));
assertFalse(template.expire("key", 10, TimeUnit.MINUTES));
assertEquals(0, template.getExpire("key", TimeUnit.MINUTES).longValue());
template.opsForHash().put("key", "hashkey", "hashvalue");
assertTrue(template.hasKey("key"));
assertTrue(template.expire("key", 10, TimeUnit.MINUTES));
assertTrue(template.getExpire("key", TimeUnit.MINUTES) > 8);
}
}
Depending on your Redis configuration, all Redis data is gone if you restart your Redis instance.
根据您的 Redis 配置,如果您重新启动 Redis 实例,所有 Redis 数据都会消失。
You should also add an assertion to expireSession
(assertTrue(cache.expireSession(session, duration));
) to make sure, the expiry worked.
您还应该向expireSession
( assertTrue(cache.expireSession(session, duration));
)添加断言以确保到期有效。