Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you get the last 10 keys redis?

Tags:

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?

like image 944
Siva Avatar asked May 15 '13 19:05

Siva


People also ask

How can I see all Redis keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

How can I get total number of keys in Redis?

The first command you can use to get the total number of keys in a Redis database is the DBSIZE command. This simple command should return the total number of keys in a selected database as an integer value. The above example command shows that there are 203 keys in the database at index 10.

Which Redis command is used to obtain all the keys in a database?

The Redis KEYS command returns all the keys in the database that match a pattern (or all the keys in the key space). Similar commands for fetching all the fields stored in a hash is HGETALL and for all fetching the members of a SMEMBERS. The keys in Redis themselves are stored in a dictionary (aka a hash table).

How do I get Redis list?

Redis Get List Items. To get elements in a Redis, use the LRANGE command. This command takes the name of the list and the index range of the element you wish to access. The command should return the values of the elements in the specified range.


1 Answers

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 
like image 167
gkamal Avatar answered Sep 20 '22 17:09

gkamal