Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all keys from Redis Cache database

I am using Redis cache for caching purpose (specifically stackexchange.Redis C# driver. Was wondering is there any ways to get all the keys available in cache at any point in time. I mean the similar thing I can do in ASP.NET cache object (below code sample)

var keys = Cache.GetEnumerator();                               
while(keys.MoveNext())
{
     keys.Key.ToString() // Key
}

Redis documentation talks about KESY command but do stackexchange.Redis have implementation for that command.

Debugging through the connection.GetDataBase() instance, I don't see any method / property for that.

Any idea?

like image 517
Rahul Avatar asked May 25 '16 11:05

Rahul


People also ask

How do I get 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.

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

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 view the contents of Redis cache?

By clicking on a Redis key name, all its contents will open in a new editor tab. With a collection type Redis key, clicking on it will reveal the individual elements under the key name. Clicking the individual element will display its contents in a new editor tab.

How do I view Redis data?

A Redis server has 16 databases by default. You can check the actual number by running redis-cli config get databases. In interactive mode, the database number is displayed in the prompt within square braces. For example, 127.0. 0.1:6379[13] shows that the 13th database is in use.


1 Answers

Function that you need is under IServer interface, and can be reached with:

ConnectionMultiplexer m = CreateConnection();
m.GetServer("host").Keys();

Note that prior to version 2.8 of redis server that will use KEYS command you mentioned, and it can be very slow in certain cases. However if you use redis 2.8+ - it will use SCAN command instead, which performs better. Also ensure that you really need to get all keys, in my practice I've never ever needed this.

like image 145
Evk Avatar answered Sep 19 '22 13:09

Evk