skip to Main Content

I am using Redis queue and adding the data using ListLeftPush and reading data using ListRightPop. It works fine I am able to get the data. But what if data has not popped out? Can we delete old data? or Can we add Value in Redis List with Expiration Time?
How to add time limit for each value while using ListLeftPush command in C#?

2

Answers


  1. It is not possible to add expire time for individual value for the sake of keeping redis simple and fast.

    you can only add expire time for individual keys i.e in ur case it is for whole list.

    Login or Signup to reply.
  2. No redis doesn’t support that. Expiration is available only for the top level keys. The closes data type/solution for your case would be sorted sets.

    • You put your expiration time(timestamp) as score while adding to sorted set(ZADD)
    • Instead of LPOP you use ZPOPMAX to get “to be last expired” element.
    • Periodically you may use ZREMRANGEBYSCORE to remove expired elements.

    For the demonstration i used smaller numbers as expiration dates.

    127.0.0.1:6379> ZADD myset 15 "a"
    (integer) 0
    127.0.0.1:6379> ZADD myset 25 "b"
    (integer) 0
    127.0.0.1:6379> ZADD myset 35 "c" 45 "d" 55 "e"
    (integer) 0
    127.0.0.1:6379> ZRANGE myset 0 -1 WITHSCORES
     1) "a"
     2) "15"
     3) "b"
     4) "25"
     5) "c"
     6) "35"
     7) "d"
     8) "45"
     9) "e"
    10) "55"
    127.0.0.1:6379> ZPOPMAX myset
    1) "e"
    2) "55"
    127.0.0.1:6379> ZREMRANGEBYSCORE myset -1 15
    (integer) 1
    127.0.0.1:6379> ZRANGE myset 0 -1
    1) "b"
    2) "c"
    3) "d"
    127.0.0.1:6379>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search