sql >> Base de Datos >  >> NoSQL >> Redis

RedisTemplate expirar no funciona

Configuré el siguiente código para realizar una prueba en 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);
    }

}

Según su configuración de Redis, todos los datos de Redis desaparecerán si reinicia su instancia de Redis.

También debe agregar una afirmación a expireSession (assertTrue(cache.expireSession(session, duration)); ) para asegurarse de que la caducidad funcionó.