Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event on key expire

I wonder if there is a feature in redis that allow me to get all expired keys (I mean some kind of event, that gives me an opportunity to take back all expire records). The purpose of it is in saving old values into another database. I've heard that it's possible using publishing mechanism, but google can't help we with this idea.

like image 438
martoskin Avatar asked Feb 01 '13 13:02

martoskin


People also ask

What happens when my GPG key expires?

When your key expires, it becomes invalid. This means: It can no longer be used to encrypt and decrypt messages. It cannot sign other keys or receive signatures from other keys.

How can I tell when my Redis expires?

First, create a key in redis and set some value in it. Now set the expiry of the key and after that just check the remaining expiry time. First, create a key in redis and set some value in it. Now set the expiry of the key, and after that just check the remaining expiry time.

How can the expiration of keys be set?

You can set an expiration time for an existing key with the expire command, which takes the name of the key and the number of seconds until expiration as arguments.

What is expiry Redis?

Keys with an expireWhen a key has an expire set, Redis will make sure to remove the key when the specified amount of time elapsed. The key time to live can be updated or entirely removed using the EXPIRE and PERSIST command (or other strictly related commands).


3 Answers

Current development version of redis contains a new feature: keyspace notifications. Documentation: http://redis.io/topics/notifications

Keyspace notifications allows clients to subscribe to Pub/Sub channels in order to receive events affecting the Redis data set in some way.

Examples of the events that is possible to receive are the following:

  • All the commands affecting a given key.
  • All the keys receiving an LPUSH operation.
  • All the keys expiring in the database 0.

Hopefully, it will make it to stable soon.

BTW, it won't be very useful in helping you save values of expired keys. When expiration event is fired, the value is gone already.

like image 167
Sergio Tulentsev Avatar answered Sep 25 '22 21:09

Sergio Tulentsev


The built-in expired event generated by keyspace notification is not accurate. See the last section of http://redis.io/topics/notifications

Keys with a time to live associated are expired by Redis in two ways:

  1. When the key is accessed by a command and is found to be expired.

  2. Via a background system that looks for expired keys in background, incrementally, in order to be able to also collect keys that are never accessed.

The expired events are generated when a key is accessed and is found to be expired by one of the above systems, as a result there are no guarantees that the Redis server will be able to generate the expired event at the time the key time to live reaches the value of zero. If no command targets the key constantly, and there are many keys with a TTL associated, there can be a significant delay between the time the key time to live drops to zero, and the time the expired event is generated.

To fully implement an accrue expire event, you might have to implement it by yourself. For example, by using a sorted list (or AVL tree) sorted by the expire time, and continuously reading the tail of list (unqueue). If the reader finds the the expire time hasn't been reached, it sleeps (expire time - now). In this way the accuracy can be controlled within 10s ms.

like image 36
user3678130 Avatar answered Sep 22 '22 21:09

user3678130


Use redis-scheduler.
You can find it here

like image 24
Mosayeb Avatar answered Sep 24 '22 21:09

Mosayeb