In Redis, we can use the OBECT IDLETIME
command to check how many seconds has passed since the last access to a given key.
Syntax
The syntax goes like this:
OBJECT IDLETIME key
Example
Here’s a basic example to demonstrate:
OBJECT IDLETIME animals
Result:
(integer) 255617
We get an integer reply with the number of seconds since the last time the key was accessed.
Let’s access the key:
LRANGE animals 0 -1
Result:
1) "Cat" 2) "Dog" 3) "Buffalo" 4) "Cow"
Now let’s run OBJECT IDLETIME
again:
OBJECT IDLETIME animals
Result:
(integer) 24
We can see that the value was reset and it has now been just 24 seconds since the key was accessed.
Let’s wait a day and run the command again:
OBJECT IDLETIME animals
Result:
(integer) 88203
One thing to remember is that the OBJECT IDLETIME
command is only available when the maxmemory-policy
configuration directive is not set to one of the LFU policies.