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

蹲街弑〆低调 提交于 2019-12-11 13:43:35

问题


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.


回答1:


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.




回答2:


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

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, e.g:

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() e.g:

redis.SetValues(allKeysAndValues);


来源:https://stackoverflow.com/questions/36861814/how-to-get-update-all-keys-and-its-values-from-redis-database-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!