Redis setting TTL on hSet Keys

12,275

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

Share:
12,275
Isengo
Author by

Isengo

XHProf, Jenkins,Gitlab CI, Docker, Shopware, Cakephp, Symfony, Laravel, Stripe, Paypal...

Updated on June 26, 2022

Comments

  • Isengo
    Isengo almost 2 years

    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?

    • thepirat000
      thepirat000 about 6 years
      short answer: no. long answer: no, you can't.
  • Isengo
    Isengo about 6 years
    Can I expire or set a TTL for the hash? Tomasz says I can put a TTL on the hash itself
  • Isengo
    Isengo about 6 years
    Which method is that? How can I do so?
  • Bhoomtawath Plinsut
    Bhoomtawath Plinsut about 3 years
    @Isengo He meant TTL on the key itself using redis.io/commands/expire. You can't expire a hash, but you can expire each key.