Redis数据结构设计,用于对基于时间的值进行排序 [英] Redis data structure design for sorting time-based values

查看:215
本文介绍了Redis数据结构设计,用于对基于时间的值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对数据流进行一些分析,并在Redis频道上发布结果。消费者订阅这些频道并获取实时数据Feed。所有历史数据分析结果都将丢失。

I'm performing some analysis on a data stream and publishing the results on a Redis channel. Consumers subscribe to these channels and get real-time data feeds. All historical data analysis results are lost.

现在我想添加在Redis中存储历史数据的功能,以便消费者可以查询这个历史数据(主要是按时间)。由于分析结果是按时间划分的,那么在Redis中存储结果的好设计是什么?

Now I want to add the ability to store historical data in Redis so that consumers can query this historical data (mainly by time). Since the analysis results are partitioned by time what would be the a good design to store the results in Redis?

推荐答案

使用redis 排序集

排序集根据分数存储数据,因此在您的情况下,只需使用毫秒的时间戳;数据将被自动排序,允许您使用开始/结束日期范围检索历史项目,以下是一个示例...

Sorted sets store data based on "scores", so in your case, just use a time stamp in millis; the data will be sorted automatically, allowing you to retrieve historical items using start/end date ranges, here's an example...

将项目添加到排序集...

Add items to a sorted set...

zadd historical <timestamp> <dataValue>

..添加一些样本数据..

..add some sample data..

 zadd historical 1 data1
 zadd historical 2 data2
 zadd historical 3 data3
 zadd historical 4 data4
 zadd historical 5 data5
 zadd historical 6 data6
 zadd historical 7 data7

开始/结束范围...

..retrieve a subset of items using start/end range...

 zrangebyscore historical 2 5

..返回...

1) "data2"
2) "data3"
3) "data4"
4) "data5"

所以,在你的情况下,如果你想检索最后一天的所有历史项目,只要这样做...

So, in your case, if you want to retrieve all historical items for the last day, just do this...

zrangebyscore historical <currentTimeInMillis> <currentTimeInMillis - 86400000>

这篇关于Redis数据结构设计,用于对基于时间的值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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