Time of creation of key in redis

TimeRedis

Time Problem Overview


Suppose I do this in redis at 13:30 20 Feb 2020,

> set foo "bar spam"
OK

I want to get time of creation of foo. Is there something like

> gettime foo
13:30 20 Feb 2020

?

Time Solutions


Solution 1 - Time

Redis doesn't store this information.

You could use a separate key:

MULTI
SET foo "bar spam"
SET foo:time "13:30 20 Feb 2020"
EXEC

GET foo:time

Solution 2 - Time

There is another, similar option to solve this, for the use case when you need timer to detect expired value without deleting the value itself:

MULTI
SET foo "bar"
SET foo:alive 1 EX 30
EXEC

Here 30 - is a desired timeout. You can then determine whether value is still "alive" with:

EXISTS foo:alive

Solution 3 - Time

I think it's possible if you know initial TTL;

you can do like this:

$init = 60; //initial time
$ttl = $redis->ttl("key"); //current ttl
$diff = $init - $ttl; //difference is the time passed after key was created
$creation = time() - $diff; //this is creation time in seconds since unix epoch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPratik DeoghareView Question on Stackoverflow
Solution 1 - TimeDonald MinerView Answer on Stackoverflow
Solution 2 - TimeDamaged OrganicView Answer on Stackoverflow
Solution 3 - TimelegaleView Answer on Stackoverflow