I am on an dead end with redis cache. I want to set an TTL on the initiation of a key. The key will be set by hSet($hash, $key, $data)
expire($key, '3600')
does not seem to work. Is there an hExpire() method?
I am on an dead end with redis cache. I want to set an TTL on the initiation of a key. The key will be set by hSet($hash, $key, $data)
expire($key, '3600')
does not seem to work. Is there an hExpire() method?
Explanation:
Redis supports expiration only on KEY level. It does not support expiration on inner element(s) of any data structure, let alone hash.
Answer:
hExpire
method/command in Redis.Update:
You can expire a whole data structure (a.k.a. a key).
One of the command to expire key is EXPIRE key seconds
.
Assuming you are using phpredis, your method call can be setTimeout($hash, 3600)
.
You need to put the TTL on the $hash
itself as you can't expire individual keys of a hash.
For anyone reading this in 2024, as mentioned in one of the earlier comments Redis setting TTL on hSet Keys, the linked pull request in the comment has been merged. Redis supports expiration at hash field level now https://redis.io/docs/latest/commands/?name=hexp
You can try TairHash, which is a redis module, similar to redis hash, but you can set expire and version for the field: https://github.com/alibaba/TairHash
© 2022 - 2024 — McMap. All rights reserved.