简体   繁体   中英

How to get ,update all keys and its values from redis database in c#?

I am using servicestack C# driver for connecting redis database which runs in 6379. I want to retrieve(GET/READ) all keys and its values from redis database(which is actually cached). I have to update two keys and its values in redisdb, is it possible to make an update without using list or hashtypes?

I want to know how to add numerous keys and values as well as update many at a time.

With respect to Redis you want multiple update, multiple get and multiple add. You can use these commands for that purpose.

Mset - Multiple Set Mget - Multiple Get Hmset - Multiple Set in Hashes. Msetnx - Multiple Set if not exist.

The most efficient way to retrieve all keys is to use the SCAN API's , eg:

var allKeys = new List<string>();
using (var redis = redisManager.GetClient())
{
    foreach (var key in redis.ScanAllKeys())
    {
        allKeys.Add(key);
    }
}

You can get all values with GetValues() to return a List of values or GetValuesMap() to return a dictionary of key values, eg:

var allKeyAndValues = redis.GetValuesMap(allKeys);

But depending on the number of keys you have you'll want to fetch them in batches.

Likewise you can set multiple keys and values with SetValues() eg:

redis.SetValues(allKeysAndValues);

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