Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis setting TTL on hSet Keys

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?

like image 920
Isengo Avatar asked Apr 27 '18 15:04

Isengo


People also ask

What is the default TTL for Redis?

There is no default TTL. By default, keys are set to live forever.

What does HSET do in Redis?

Redis HSET command is used to set field in the hash stored at the key to value. If the key does not exist, a new key holding a hash is created. If the field already exists in the hash, it is overwritten.

What is difference between HSET and set in Redis?

The only difference between the commands HSET and HMSET is the return value of the commands.


1 Answers

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).

like image 94
sazzad Avatar answered Oct 11 '22 17:10

sazzad