You could store key/values in Redis differently to achieve this, by just adding a prefix or namespace to your keys when you store them e.g. "hset_"
Get a key/value
GET hset_key
equals toHGET hset key
Add a key/value
SET hset_key value
equals toHSET hset key
Get all keys
KEYS hset_*
equals toHGETALL hset
Get all vals should be done in 2 ops, first get all keys
KEYS hset_*
then get the value for each keyAdd a key/value with TTL or expire which is the topic of question:
SET hset_key value
EXPIRE hset_key
Note: KEYS
will lookup up for matching the key in the whole database which may affect on performance especially if you have big database.
Note:
KEYS
will lookup up for matching the key in the whole database which may affect on performance especially if you have big database. whileSCAN 0 MATCH hset_*
might be better as long as it doesn't block the server but still performance is an issue in case of big database.You may create a new database for storing separately these keys that you want to expire especially if they are small set of keys.
Thanks to @DanFarrell who highlighted the performance issue related to
KEYS