How can I get a key count for StackExchange.Redis?

4.6k Views Asked by At

I have an azure redis cache. I've set it up so that I can do stringGet key setting based off this example. It works pretty great.

However, I want to know if the cache is empty or not, or if there are any entries of (x) type of a C# object I have. I currently want to see if I can get a key count. I haven't found any solutions for this.

My only idea is to do a key scan that would search for every key (a get all) and then do a count. But that seems inefficient. Is there a more "meta" data style solution?

Thanks!

1

There are 1 best solutions below

1
On

Per my understanding, you could leverage IServer.Keys to retrieve all keys with matching pattern as follows:

var endpoints=ConnectionMultiplexer.GetEndPoints();
var server = ConnectionMultiplexer.GetServer(endpoints.First());
var keys = server.Keys();

For more details about keys scanning, you could refer to this tutorial.