Redis setting TTL on hSet Keys
Asked Answered
W

4

11

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?

Wandering answered 27/4, 2018 at 15:2 Comment(2)
short answer: no. long answer: no, you can't.Highborn
2024-03-31: Redis started implementing hash field expiration, and it's planned for an upcoming release. github.com/redis/redis/pull/13172Lapstrake
G
21

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:

  1. No. There is no hExpire method/command in Redis.
  2. You're trying expire an inner element in a hash. This is not possible 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).

Gaberlunzie answered 27/4, 2018 at 18:50 Comment(1)
Can I expire or set a TTL for the hash? Tomasz says I can put a TTL on the hash itselfWandering
A
0

You need to put the TTL on the $hash itself as you can't expire individual keys of a hash.

Anna answered 27/4, 2018 at 15:31 Comment(3)
Which method is that? How can I do so?Wandering
@Wandering He meant TTL on the key itself using redis.io/commands/expire. You can't expire a hash, but you can expire each key.Elute
@Gaberlunzie Why does Redis not support the EX parameter for HSET like it does for SET ? It is constraining to have to make a second EXPIRE commandSpurt
A
0

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

Alluvial answered 2/7 at 19:10 Comment(0)
W
-2

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

Wigan answered 13/7, 2021 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.