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?
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With