In Redis how would you update a key and reset the original TTL?
Asked Answered
C

3

8

I'm interacting with Redis through PHPRedis (actually a higher level wrapper built around PHPRedis). I want to update a key and reset it in the database, but the TTL should be reset to the original value at the current point in the program my Class doesn't know what the original TTL.

So say the original TTL was 12 hours. I want to do something like this:

redis->get($key)
$original_ttl = // figure it out
$new_value = something
redis->set($key, $new_value, $original_ttl)

Then we end up with the original key referencing a new value and another 12 hours ttl. Is this possible?

Coursing answered 6/3, 2014 at 18:3 Comment(0)
B
9

Just use two commands: a SET to update the value and then an EXPIRE to update the TTL.

Update: To retrieve the original TTL you have to store it in a separate key. As far as I know, you can get the current TTL, but not its initial value.

So it would look like this in pseudo code (REDIS commands are in capitals):

SET myKey value
EXPIRE myKey 3600
SET myKey:ttl 3600

to fix the TTL to 3600s

and then

SET myKey newValue
ttlvalue = GET mykey:ttl
EXPIRE myKey ttlvalue

Update 2 :

My response may be improved using Agis' suggestion to use SETEX, which sets a value for a given key and its expiration date in one operation. So it would become:

SETEX myKey 3600 value
SET myKey:ttl 3600

to fix the TTL to 3600s

and then

ttlvalue = GET mykey:ttl
SETEX myKey ttlvalue newValue

to update the value and reset its TTL

Botticelli answered 6/3, 2014 at 21:11 Comment(3)
Right - but I need to get the original TTL that was set when the key was inserted so I can reset it to the original value.Coursing
I did not understand this at first reading. I updated my answer.Botticelli
@asolberg: I improved the answerBotticelli
M
4

You could define the initial TTL value in a constant somewhere in your code and use it everytime you set a key.

Then use SETEX to both set the value and the expiration time of the key:

define('TTL', 120); // 2 minutes
redis->setex($key, TTL, $new_value);

The equivalent command in Redis would be like:

> SETEX mykey 120 "myvalue"

If you want to set the time in milliseconds instead of seconds, then use PSETEX.

Marguerite answered 7/3, 2014 at 9:17 Comment(0)
V
0

as i see your tag for php so your code can be :

    $id = 3453451244;
    $int = 1;
    if ($redis->get("active:$id") === false) {
    $redis->set("active:$id", $int);
    $redis->expire("active:$id", 86400);
  } else {
    $_timePassed = $redis->ttl("active:$id");
    $redis->set("active:$id", $redis->get("active:$botid") + 1);
    $redis->expire("active:$id", $_timePassed);
  }

explain: first set key and its expire for me i set it one day you can use :

function seconds2human($ss) {
$s = $ss%60;
$m = floor(($ss%3600)/60);
$h = floor(($ss%86400)/3600);
$d = floor(($ss%2592000)/86400);
$M = floor($ss/2592000);

return "$M months, $d days, $h hours, $m minutes, $s seconds";
}

to convert seconds => $M months, $d days, $h hours, $m minutes, $s seconds

number two : you must save ttl in a variable ..($_timePassed) and use set&expire to set you new value...

if you are want to reset into your orginal time simply change ($_timePassed) variable to your orignal time in seconds

$_timePassed = 86400; // 1 day

i use this library for redis on php: https://github.com/phpredis/phpredis#expire-settimeout-pexpire

Hope that answer your Q .

Valuer answered 1/8, 2021 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.