Hello, how can I set the retention time for the Redis hash table. $redis->hset('Ключ', 'Значение 1','Значение 2')) , namely for this key and Value 1, using php.

  • What library do you use to interact with Redis? - cheops

1 answer 1

If you use the phpredis library, then to set the key lifetime, you can use the setEx() method, the second parameter of which you can pass the number of seconds the key should live:

 $redis->setEx('ключ', 3600, 'значение 1'); 

Alternatively, you can set the value and set the key lifetime using a separate command setTimeout()

 $redis->hset('Ключ', 'Значение 1','Значение 2')); $redis->setTimeout('Ключ', 3600); 
  • Works great for set, but doesn't work for hset. - Mikhail Lobanov
  • @ MikhailLobanov and if you explicitly set the lifetime using the setTimeout() method also does not work? - cheops
  • Can you please an example, otherwise I did not quite understand. - Mikhail Lobanov
  • @ MikhailLobanov corrected the answer, gave an example with setTimeout() . - cheops
  • Does not work, setTimeout. - Mikhail Lobanov