skip to Main Content

I have a Redis cache, I have set a few keys in that, and now I need to get all the keys and their values. It seems that there is no direct method to get this.

I can see only StringGet(string key) method which takes a key as a parameter. But no method there for all the keys

Edit:

I have tied the below code, but it’s giving an exception on 3rd line.

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase db = redis.GetDatabase();

var keys = redis.GetServer("localhost").Keys();

string[] keysArr = keys.Select(key => (string)key).ToArray();

foreach (string key in keysArr)
{
    Console.Write(db.StringGet(key));
}

Exception:

the specified endpoint is not defined

2

Answers


  1. Chosen as BEST ANSWER

    You can call the Keys method to get all the keys, remember you need to pass allowAdmin=true" in the Connect method.

    using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379,allowAdmin=true"))
            {
                IDatabase db = redis.GetDatabase();
    
                var keys = redis.GetServer("localhost", 6379).Keys();
    
                string[] keysArr = keys.Select(key => (string)key).ToArray();
    
                foreach (string key in keysArr)
                {
                    Console.WriteLine(db.StringGet(key));
                }
            }
    

  2. You can try this way

    IServer server = Connection.GetServer("yourcache.redis.cache.windows....", 6380);
    foreach (var key in server.Keys())
    {
       Console.WriteLine(key);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search