Linus_30
Linus_30

Reputation: 191

Which redis data structure to use to store values as set with each value to have an expiry time

I've a use case where I need to store values for eg. SADD key *values

but I also want to persist the values only for certain duration say 1 day after which the specific value should get expired.

Please suggest how can this be achieved using redis.

Upvotes: 0

Views: 63

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50112

Redis' expiration is implemented at key level, not inside the value. As an alternative, use a Sorted Set, with the score of each member being its expiration timestamp.

You will have to "expire" elements manually, so periodically call ZREMRANGEBYSCORE to remove all elements with a timestamp lower than now.

Upvotes: 1

Related Questions