Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RedisTemplateExpire不起作用_Java_Unit Testing_Session_Redis - Fatal编程技术网

Java RedisTemplateExpire不起作用

Java RedisTemplateExpire不起作用,java,unit-testing,session,redis,Java,Unit Testing,Session,Redis,我正在尝试在RedisTemplate中测试expire方法。例如,我将会话存储在redis中,然后尝试检索会话并检查值是否相同。对于expire会话,我使用redisTemplate的expire方法;对于获取过期会话,我使用getExpire方法。但它不起作用。如何测试存储在redis中的值 //without import and fields public class Cache() { private StringRedisTemplate redisTemplat

我正在尝试在RedisTemplate中测试expire方法。例如,我将会话存储在redis中,然后尝试检索会话并检查值是否相同。对于expire会话,我使用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));    
    }    
}
但测试失败,断言错误:

预期:16实际:0

更新:
我想,getExpire方法不起作用,但实际上expire方法不起作用。它返回false。redisTemplate是自动连接到测试类的Springbean。TestCache类中还有许多其他测试方法可以正常工作。

我设置了以下代码,以便在GetS 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);
    }

}
根据您的Redis配置,如果重新启动Redis实例,所有Redis数据都将消失


您还应该向expireSession assertTruecache.expireSessionsession,duration添加断言;为了确保有效期有效。

我设置了以下代码,在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);
    }

}
根据您的Redis配置,如果重新启动Redis实例,所有Redis数据都将消失


您还应该向expireSession assertTruecache.expireSessionsession,duration添加断言;为确保有效期有效。

密钥是否存在,是否要过期?您的代码不包含在其中创建密钥的部分。expire返回true,如果可以应用expire。有关更多详细信息,请参阅。session,我将其作为参数发送给redisTemplate。expire是一个键字符串session=session_123;我的意思是,在测试您的redis实例时,密钥会话_123是否存在。它必须存在。在我的代码中,我使用该密钥终止会话。我在上课前启动redis server,下课后停止。我不知道为什么,但redis Template从expire Method返回false密钥是否存在,是否要过期?您的代码不包含在其中创建密钥的部分。expire返回true,如果可以应用expire。有关更多详细信息,请参阅。session,我将其作为参数发送给redisTemplate。expire是一个键字符串session=session_123;我的意思是,在测试您的redis实例时,密钥会话_123是否存在。它必须存在。在我的代码中,我使用该密钥终止会话。我在上课前启动redis服务器,下课后停止。我不知道为什么,但redis模板从expire Method返回false为什么需要调用flushAll?我有一些代码在每次Tomcat重新启动时清除Redis缓存。我发现代码中有一个flushAll返回redistemplate,这只是为了让测试用例创建一个干净的状态。为什么需要调用flushAll?我有一些代码在每次Tomcat重新启动时清除Redis缓存。我发现代码中有一个flushAll返回redistemplate,这只是为了让测试用例创建一个干净的状态。