博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis data structure design for sorting time-based values
阅读量:6279 次
发布时间:2019-06-22

本文共 2180 字,大约阅读时间需要 7 分钟。

  hot3.png

Question:

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.

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?

Answer:

Use 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 

..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

..returns...

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 

Additional

(1) What if two data inserts in the same milisecond?

While members are unique in a sorted set, scores (time stamps in this case) may be repeated.

(2) What if each data value is not unique?

you can add some random string before or after your data to keep it not repeated.

I think in your last code example you'd want to reverse the range, i.e. zrangebyscore historical <currentTimeInMillis - 86400000> <currentTimeInMillis> since it looks like it expects the lower value first.

Redis command returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores. The elements having the same score are returned in lexicographical order (this follows from a property of the sorted set implementation in Redis and does not involve further computation).

Reference:

转载于:https://my.oschina.net/dabird/blog/800613

你可能感兴趣的文章
去 TMD 互联网思维,性价比而已
查看>>
如何手动删除Oracle 11g数据库
查看>>
懒人促进社会进步 - 5种索引的原理和优化Case (btree,hash,gin,gist,brin)
查看>>
《深入实践Spring Boot》一3.4 视图设计
查看>>
《设计模式解析(第2版•修订版)》目录—导读
查看>>
《Web前端开发精品课 HTML与CSS进阶教程》——2.2 标题语义化
查看>>
Java核心技术卷I基础知识3.5.3 强制类型转换
查看>>
可与Mirai比肩的恶意程序Hajime,竟是为了保护IoT设备?
查看>>
《Spring Data 官方文档》6. Cassandra 存储库
查看>>
聊聊并发(十)生产者消费者模式
查看>>
R语言数据挖掘2.2.4.2 FP-growth算法
查看>>
人工智能概念诞生60年,哪些大牛堪称“一代宗师”?
查看>>
《游戏大师Chris Crawford谈互动叙事》一9.5 真实案例
查看>>
Mybatis与Spring整合连接MySQL
查看>>
GCC知识
查看>>
实验4 IIC通讯与EEPROM接口
查看>>
几个smarty小技巧
查看>>
Cocos2d-x3.2 Grid3D网格动作
查看>>
Java (for循环综合应用)
查看>>
NodeJs——(10)REST风格的路由规则
查看>>