How would you get the last 10 keys redis?
20,986
Solution 1
You will need to maintain it as another list using the following commands.
Add new key to the front of the list
LPUSH last10keys key
Retain only the last 10
LTRIM last10keys 0 9
Get the last keys - will return 10 or less
LRANGE mylist 0 9
Solution 2
As a workaround if I don't want to change anything in the cache, I tail the AOF file to see what's the latest change there.
tail -f /var/lib/redis/appendonly.aof
From there, you can see the key, value and command used.
Related videos on Youtube
Author by
Siva
Updated on July 09, 2022Comments
-
Siva almost 2 years
Let's say I have a database with 1,000,000 keys. Is there a way to know the last 10 keys from that database?
-
Tommaso Barbugli about 11 yearsnope, you have to implement it yourself
-
Siva about 11 yearsAnd how would you go about doing this?
-
Tommaso Barbugli about 11 yearsyou can store keys in a list or in a sorted set
-
Siva about 11 yearsI see. So I would need make a list that stores all the available keys?
-
Tommaso Barbugli about 11 yearswell depending on the space you want to invest with this.I would keep the size of this list to a fixed length
-
Siva about 11 yearsWould there be a way to even get the last key in the database?
-
Eli about 11 years@Sivapriyan can you tell us more about what you need the last 10 keys in particular for? Maybe we can recommend a workaround. As Tommaso Barbugli said, to do what you want, you'd need to use a secondary structure like a list or a sorted set (slower) to get at the last n keys.
-
akonsu about 11 yearswhat is "last" in "the last 10 keys"?
-
-
Zitrax over 9 yearsThis requires appendonly to be set to yes in the config file. That is not the default.