问题
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