Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do not Redis provide a batch set expire time operation on multiple keys?

Tags:

redis

I want to set multiple keys with an expire time, but it seems I have to expire every keys one by one.Why do not Redis provide an operation just like "mset"?

like image 690
halostack Avatar asked Jun 04 '13 07:06

halostack


People also ask

Can we have multiple keys in Redis cache?

Having multiple keys point to same value is not supported in Redis for now, see issue #2668. You would need a workaround. You could implement that logic in your client code, or in custom Lua scripts on server, and have your client code use those scripts (but I don't know enough about that to provide details).

Do Redis keys expire?

Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.

Can you define an expiration time of a field in a hash Redis?

All the Redis objects have an expiration time. By default, the value is set to never. However, by changing this value, we can set Redis keys that will expire automatically after a fixed amount of time. Once the expiration time is passed, the key is deleted from the database.


2 Answers

You can use one script as it.

EVAL 'for i, name in ipairs(redis.call("KEYS", "0*")) do redis.call("EXPIRE", name, 10); end' 0
like image 169
Rudge Avatar answered Sep 28 '22 04:09

Rudge


Because this operation is not so frequent and you can easily simulate it by:

  • a server-side Lua script

or

  • pipelining several expire commands

Whatever the chosen solution, it will only generate a single roundtrip to the redis server.

like image 29
Didier Spezia Avatar answered Sep 28 '22 04:09

Didier Spezia