Get the last time a given Redis key was accessed
Asked Answered
S

2

24

I'd like to view the time of most recent access for a specific key on my redis server.

I know that this information is stored for each key because it is used in redis's LRU algorithm for eliminating old keys.

Is there an easy way to see this information for a given key?

Stagecoach answered 7/10, 2014 at 21:27 Comment(1)
@Javier I appreciate the sentiment, but LRU actually stands for "Least Recently Used", at least in the context of the LRU algorithm. en.wikipedia.org/wiki/Cache_algorithmsStagecoach
S
39

You can use the OBJECT IDLETIME command for this purpose. It returns the number of seconds since the key was accessed, but If you need the time just subtract the reply from now().

Sanjuana answered 7/10, 2014 at 22:37 Comment(2)
But this will not give epoch... Also can't separate last write compare to last read...Waste
True, far from ideal but it is what it isSanjuana
C
5

Itamar Haber's answer is definitely the best, but I believe there is an other way.

You can use the DEBUG OBJECT command, although as its name indicates it is a debug command and should not (really) be used. Its output gives you the LRU.

Keep in mind that it should definitely not be used in production but rather as a tool to help you understand what is going on.

Chummy answered 8/10, 2014 at 10:45 Comment(2)
Very true. Also keep in mind that DEBUG is usually blocked (although most likely OBJECT is prohibited as well ;)) in well-maintained production environments.Sanjuana
@ItamarHaber Why OBJECT would be blocked in a well-maintained production system?Chiquita

© 2022 - 2024 — McMap. All rights reserved.