skip to Main Content

Can someone explain me how to store key/value pair in redis with unlimited exipire time?

I was trying to find some information at stackoverflow and google and I found nothing.

I’m using redisTemplate and I see there is only expire method there with timeout to specify. I can eventually set the timeout to value like 999999999 days, but I think thats not the best solution.

2

Answers


  1. Chosen as BEST ANSWER

    Finally I found two answers to this question - at first I can use expire method with -1 value as a timeout:

    redisTemplate.expire("mykey", -1);
    

    and the second way is to use persist method on key:

    redisTemplate.persist("mykey");
    

    This will remove the expiry time for the "mykey" key, effectively setting it to never expire.


  2. By default, no expiry is set on the keys. That is, if you do not specify an expiry yourself, your key will be stored indefinetely without any expiry.

    set myname Ankit
    OK
    
    ttl myname
    (integer) -1
    

    A return of -1 tell us that there is no expiry set on the key myname

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search