Is it possible in Redis to set TTL (time to live) not for a specific key, but for a member for a set?
I am using a structure for tags proposed by Redis documentation - t
No, this isn’t possible (and not planned either). The recommended approach is to use an ordered set with score set to timestamp and then manually removing expired keys. To query for non-expired keys, you can use ZRANGEBYSCORE $now +inf
, to delete expired keys, ZREMRANGEBYSCORE -inf $now
will do the trick.
In my application, I simply issue both commands every time I query the set. I also combine this with (long) expiration time on the set itself to eventually purge unused sets.
This article walks through it in more detail.