Time of creation of key in redis

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

?


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

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

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