The Redis TOUCH
command alters the last access time of a key or keys. It allows us to alter the access time of a key without accessing it directly.
Syntax
TOUCH key [key ...]
Example
Suppose we create a key:
SET player "Jet"
Result:
OK
And we then use OBJECT IDLETIME
to find out how many seconds has gone past since that key was accessed:
OBJECT IDLETIME player
Result:
(integer) 12
So far it’s been 12 seconds. If we wait a bit, and run it again, the idle time increases:
OBJECT IDLETIME player
Result:
(integer) 102
The idle time increases with each second that passes. Now it’s been 102 seconds.
Now let’s use TOUCH
to reset the idle time:
TOUCH player
Result:
(integer) 1
We get an integer reply of 1, which means that one key was touched.
Now let’s quickly run OBJECT IDLETIME
again:
OBJECT IDLETIME player
Result:
(integer) 8
It’s back down to 8 and increasing again with every second.
Multiple Keys
We can apply TOUCH
to multiple keys at once.
Let’s check another key’s idle time:
OBJECT IDLETIME age
Result:
(integer) 536642
This key is a lot older than the previous one and it hasn’t been accessed in a while.
Let’s use TOUCH
against both keys:
TOUCH player age
Result:
(integer) 2
This time we get an integer reply of 2.
And then check their idle time again:
OBJECT IDLETIME player
Result:
(integer) 7
And:
OBJECT IDLETIME age
Result:
(integer) 14
I guess 7 seconds must’ve passed between the time I ran the first one and the second.
Non-Existent Keys
If a specified key doesn’t exist, then it’s ignored:
TOUCH nonexistentkey
Result:
(integer) 0