6
votes

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?

3

3 Answers

5
votes

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

4
votes

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.

0
votes

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 .