简体   繁体   中英

How to get all the keys values from Redis Cache in C#?

I have redis cache, I have set few keys in that, 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 key as paramater. But no method there for all the keys

Edit:

I have tied the below code, but its giving 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

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));
            }
        }

You can try this way

IServer server = Connection.GetServer("yourcache.redis.cache.windows....", 6380);
foreach (var key in server.Keys())
{
   Console.WriteLine(key);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM