如何从C#中的Redis数据库获取,更新所有键及其值? [英] How to get ,update all keys and its values from redis database in c#?

查看:2027
本文介绍了如何从C#中的Redis数据库获取,更新所有键及其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用servicestack C#驱动程序连接在6379中运行的redis数据库. 我想从redis数据库(实际上已缓存)中检索(获取/读取)所有键及其值. 我必须在redisdb中更新两个键及其值,是否可以在不使用列表或哈希类型的情况下进行更新?

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.

推荐答案

检索所有密钥的最有效方法是使用

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

您可以使用GetValues()获取所有值以返回值列表,或使用GetValuesMap()获取键值字典,例如:

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.

同样,您可以使用SetValues()设置多个键和值,例如:

Likewise you can set multiple keys and values with SetValues() e.g:

redis.SetValues(allKeysAndValues);

这篇关于如何从C#中的Redis数据库获取,更新所有键及其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆