简体   繁体   English

如何使用stackexchange.redis从多个键中获取所有哈希元素

[英]How to get all hash elements from multiple key using stackexchange.redis

I'm evaluating redis, and an trying to use a lua script to get multiple hash keys from redis in one call. 我正在评估redis,并尝试使用lua脚本在一次调用中从redis获取多个哈希键。

I can't seem to get it to work. 我似乎无法正常工作。 The code below returns an array of items to my c# code, but I can't seem to get at the hash results. 下面的代码将项数组返回到我的C#代码中,但是我似乎无法获得哈希结果。 the redisResult returned is an array and each element of the array is an empty array. 返回的redisResult是一个数组,并且该数组的每个元素都是一个空数组。 I expected each element to be an array of the hash values. 我希望每个元素都是哈希值的数组。

        int key1 = 1;
        var sb = new StringBuilder();
        var compositeKeys = new List<RedisKey>();
        for (int key2 = 1; key2 < 1000; key2++)
        {
            compositeKeys.Add((RedisKey)GetRedisKey2(key1, key2));
        }


        sb.AppendLine("local collate = function(key)");
        sb.AppendLine("local raw_data = redis.call('HGETALL', key)");
        sb.AppendLine("local data = {}");
        sb.AppendLine("for idx = 1, #raw_data, 2 do");
        sb.AppendLine(" data[raw_data[idx]] = raw_data[idx + 1]");
        sb.AppendLine("end");
        sb.AppendLine("return data;");
        sb.AppendLine("end");

        sb.AppendLine("local data = {}");
        sb.AppendLine("for _, key in pairs(KEYS) do");
        sb.AppendLine("  data[_] = collate(key)");
        sb.AppendLine("end");

        sb.AppendLine("return data");

        var results = _redDb.ScriptEvaluate(sb.ToString(), compositeKeys.ToArray());

EDIT: to better explain what I'm trying to do: This will be used for looking up stock market data, and there are three use cases for getting the data out of the cache. 编辑:为了更好地解释我正在尝试做的事情:这将用于查找股市数据,并且有三种用例,用于将数据从缓存中取出。

  1. Get a single item by symbol and time 通过符号和时间获取单个项目
  2. Get a range of historical data for a single symbol 获取单个符号的一系列历史数据
  3. Get a range of symbols from specific time 获取特定时间的一系列符号

Each data point has about 30 fields of data about the stock at that time. 每个数据点当时都有大约30个有关库存的数据字段。 I am storing it using a key that combine the symbol and time, and hash fields for each field in my object. 我使用结合了符号和时间以及对象中每个字段的哈希字段的键来存储它。 The performance I am receiving for a single stock lookup is great, but I am having trouble with looking up a range of values. 单次库存查询所获得的性能很好,但是在查找一系列值时遇到了麻烦。

I implemented the pipelined approach described in this question: StackExchange.Redis: Batch access for multiple hashes . 我实现了此问题中描述的流水线方法: StackExchange.Redis:多个哈希的批处理访问 The performance is OK, but not better than what I get today out of SQL Server. 性能还可以,但并不比我今天从SQL Server中获得的性能要好。 I'd like to benchmark the performance using the LUA scripting approach, but I must be doing something wrong. 我想使用LUA脚本方法对性能进行基准测试,但是我必须做错了什么。

Rather than focus on the question title (mainly as I'm not convinced you'll get the performance gains you're looking for), I'll suggest another approach that focuses on the objectives that you added to your question later: 与其专注于问题标题(主要是因为我不相信您会获得想要的性能提升),我将建议另一种专注于您稍后添加到问题中的目标的方法:

  1. Get a single item by symbol and time 通过符号和时间获取单个项目
  2. Get a range of historical data for a single symbol 获取单个符号的一系列历史数据
  3. Get a range of symbols from specific time 获取特定时间的一系列符号

Given your interest in time based ranges, you may find that sorted sets work better. 鉴于您对基于时间范围的兴趣,您可能会发现排序集的效果更好。

A sorted set consists of elements where each element has a score (a double) and a value (any string, eg JSON, object serialized using protobuf). 排序集由元素组成,其中每个元素都有一个分数(双精度)和一个值(任何字符串,例如JSON,使用protobuf序列化的对象)。

As such you could create a set per stock and add elements to your set where the score is the trade time in milliseconds(*) and the value is some string that describes your trade. 这样,您可以为每只股票创建一个集合,并在集合中添加元素,其中得分是交易时间(以毫秒(*)为单位),值是描述您交易的一些字符串。 eg, 例如,

ZADD stocks.GOOG 1459956490731 {LastTradePrice:743.16,SomeOtherUsefulInfo:123}

and then to query for all trades within a given time range, 然后查询给定时间范围内的所有交易,

ZRANGE stocks.GOOG <start-time-in-ms> <end-time-in-ms> WITHSCORES

To get a range of symbols for a specific time you'd need an extra set where you add one element per stock that that should be returned, eg 要获得特定时间的一系列交易品种,您需要一个额外的集合,即在每只股票中添加一个应返回的元素,例如

ZADD stocks.active 1459956490731 GOOG
ZADD stocks.active 1459956490731 IBM
ZADD stocks.active 1459956490731 AAPL

Hope this helps! 希望这可以帮助!

(*) for example, (*) 例如,

var startOfTime = new DateTime(2016, 1, 1); 
var tradeTime = DateTime.Now; 
var ms = (tradeTime - startOfTime).TotalMilliseconds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM