简体   繁体   English

RedisTemplate过期不起作用

[英]RedisTemplate expire doesn't work

I'm trying to test expire method in RedisTemplate. 我正在尝试测试RedisTemplate中的expire方法。 For example, I store session in redis, and than try to retrieve session and check that values are the same. 例如,我将会话存储在redis中,然后尝试检索会话并检查值是否相同。 For expire session I use expire() method of redisTemplate and for getting expired session I use getExpire() method. 对于过期会话,我使用redisTemplate的expire()方法,对于过期会话,我使用getExpire()方法。 But it doesn't work. 但这是行不通的。 How can I test value, that stored in redis? 我如何测试存储在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. 更新:我以为,getExpire()方法不起作用,但实际上expire()方法不起作用。 It returns false. 返回false。 redisTemplate is a spring Bean that autowired to test class. redisTemplate是一个自动连接到测试类的spring Bean。 There are many other test methods in TestCache class that work correctly. TestCache类中还有许多其他测试方法可以正常工作。

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. 您还应该在expireSessionexpireSessionassertTrue(cache.expireSession(session, duration)); )中添加一个断言,以确保到期有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM