在redis中创建密钥的时间

Pra*_*are 44 time redis

假设我在redis中执行此操作13:30 20 Feb 2020,

> set foo "bar spam"
OK
Run Code Online (Sandbox Code Playgroud)

我想有时间创作foo.有没有类似的东西

> gettime foo
13:30 20 Feb 2020
Run Code Online (Sandbox Code Playgroud)

Don*_*ner 63

Redis不存储此信息.

您可以使用单独的密钥:

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

GET foo:time
Run Code Online (Sandbox Code Playgroud)


Dam*_*nic 10

对于用例需要定时器来检测过期值而不删除值本身时,还有另一个类似的选项来解决这个问题:

MULTI
SET foo "bar"
SET foo:alive 1 EX 30
EXEC
Run Code Online (Sandbox Code Playgroud)

这里30- 是一个期望的超时.然后,您可以使用以下命令确定值是否仍为"活动":

EXISTS foo:alive
Run Code Online (Sandbox Code Playgroud)


leg*_*ale 5

如果您知道初始 TTL,我认为这是可能的;

你可以这样做:

$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
Run Code Online (Sandbox Code Playgroud)