如何使用键模式和堆栈交换Redis库获取值列表

问题描述

在控制台应用程序上工作时,我有一个单例类,其中包含一些通用集合。 此集合以前保存在内存中,我们正尝试使用堆栈交换redis库将其移至redis。

基类为:

public class QueueParamDTO
{
    public string Queue { get; set; }
    public int Max { get; set; }
    public int Calls { get; set; }
    public int Holdtime { get; set; }
    public int TalkTime { get; set; }
    public int Completed { get; set; }
    public int Abandoned { get; set; }
    ...
} 

该类的属性Queue的值是唯一的,因此我使用它来构建密钥,并且该值是上述对象的序列化字符串。

RedisSingleton.Connection.GetDatabase().StringSet($"queue:{queueParam.Queue}",JsonConvert.SerializeObject(queueParam));

我能够读取单个值并反序列化对象。还可以使用模式检索键的完整列表:

var keys = server.Keys(pattern: "queue:*",pageSize: 100);

我该如何对值执行相同的操作,这意味着使用上述特定键模式获取值列表?

解决方法

首先获取要从redis检索的键列表,并将其转换为RedisKey[]的数组:

RedisKey[] queueKeys = RedisSingleton.Server.Keys(pattern: "queue:*").ToArray();

我能够检索RedisValue[]的列表:

RedisValue[] queueValues=RedisSingleton.Connection.GetDatabase().StringGet(queueKeys);

最后,我选择并反序列化为QueueParamDTO对象的列表:

List<QueueParamDTO> queues = queueValues.Select(qv => JsonConvert.DeserializeObject<QueueParamDTO>(qv)).ToList();

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...