How to “EXPIRE” the “HSET” child key in redis?

前端 未结 11 2042
予麋鹿
予麋鹿 2020-11-30 21:10

I need to expire all keys in redis hash, which are older than 1 month.

11条回答
  •  不思量自难忘°
    2020-11-30 21:45

    You could use the Redis Keyspace Notifications by using psubscribe and "__keyevent@__:expired".

    With that, each time that a key will expire, you will get a message published on your redis connection.

    Regarding your question basically you create a temporary "normal" key using set with an expiration time in s/ms. It should match the name of the key that you wish to delete in your set.

    As your temporary key will be published to your redis connection holding the "__keyevent@0__:expired" when it expired, you can easily delete your key from your original set as the message will have the name of the key.

    A simple example in practice on that page : https://medium.com/@micah1powell/using-redis-keyspace-notifications-for-a-reminder-service-with-node-c05047befec3

    doc : https://redis.io/topics/notifications ( look for the flag xE)

提交回复
热议问题