Reputation: 399
I have an application writes the keys into Redis without specifying the expirations for keys. It is not possible changing the application, but I want to configure Redis to keep only the keys for past 24 hours and delete the old keys.
HOW?
Upvotes: 0
Views: 738
Reputation: 991
AFAIK there is no way to configure Redis to keep only the keys for past 24 hours and delete the old keys, as you said, at least you set up a TTL, but there is a trick you could do.
I'm assuming you cannot change anything in the application you are telling us about... so you will need to create a script/command/application which connects to Redis server every small time interval, let's say, 1 minute. The time interval will depend on how many keys you suppose to have on average in Redis.
The application is simple, you only have to iterate over all keys and use three commands:
So the first time you run the command all the current keys in Redis will take a TTL of 24 hours, after this time they will be removed. The second execution of the command will assign a 24 hours TTL only to new keys which didn't exist in the first execution of the command and so on.
You have to take into account if the number of keys is huge, in the order of several million, you can have some problems of memory and performance so in this case, I suggest retrieving the keys using wildcards to get the keys by groups, like KEYS a* or keys 1*, depending the patter you use to key names. Here you could create a daemon which never stops and iterates constantly for every group.
Using KEYS command with a huge number of keys is not recommended in production environments but you can use this kind of workarounds I suggest before.
Upvotes: 1
Reputation: 49942
To expire keys after 24 hours, you'll need to explicitly set the TTL for each of them.
Upvotes: 1