TTL for a set member

后端 未结 3 1758
北荒
北荒 2021-01-30 01:54

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

相关标签:
3条回答
  • 2021-01-30 02:42

    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.

    0 讨论(0)
  • 2021-01-30 02:43

    It is not possible to directly expire items in list, sets, or zsets.

    You need to implement a mechanism to be notified when the master item expires, so that you can maintain the corresponding sets accordingly.

    See the answer to this question, I think it applies to your use case (replace session by id, and user by tag):

    Redis, session expiration, and reverse lookup

    0 讨论(0)
  • 2021-01-30 02:48

    You can not expire items in a list directly, but it's possible to register a lua script in your redis server and add this functionality to redis. Redis Expiration based on value

    0 讨论(0)
提交回复
热议问题