Quantcast
Channel: RedisTemplate expire doesn't work - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by mp911de for RedisTemplate expire doesn't work

$
0
0

I set up following code to perform a test on 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.

You should also add an assertion to expireSession (assertTrue(cache.expireSession(session, duration));) to make sure, the expiry worked.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images