想使用Redis作为事件统计存储 [英] Want to use Redis as an events statistics store

查看:32
本文介绍了想使用Redis作为事件统计存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Redis 真的很感兴趣,我有一个想法,想知道它是否是一个合适的用例,或者它是否对数据存储没有任何其他建议.此外,任何有关存储数据的提示将不胜感激.

I am really interested in Redis, I have an idea and wanted to know if it is a suitable use case, or if it is not any other suggestions on a data store. Also any tips on storing the data would be appreciated.

我的想法只是一个简单的事件系统,所以一个事件发生了,它存储在redis中,如下

My idea is just a simple event system so an event happens and it is stored in redis as follows

键 |价值[unixtimestamp]:[系统]:[事件] |[结果]

Key | Value [unixtimestamp]:[system]:[event] | [result]

数据可以是任何实时分析的销售、印象、错误、API 响应时间、页面加载时间.然后我希望能够根据这些数据制作图表.

The data could be anything sales, impressions, errors, api response times, page load times any real time analytics. I then want to be able to make graphs based on that data.

推荐答案

这不是一个理想的设计,因为它不能有效地支持你的读取模式,而且如果你的 [result] 很短,它可能会浪费 RAM/小的.相反,以以下方式使用 Redis 的 sorted sets 以时间戳作为分数:

This isn't an ideal design because it won't support your read pattern effectively and it will probably wasteful in terms of RAM if your [result] is short/small. Instead, look into using Redis' sorted sets with the timestamp as score, in the following fashion:

ZADD [system]:[event] [timestamp] [result]

请注意,集合成员必须是唯一的,因此如果 [result] 的基数较低,请通过将时间戳连接到它(并在绘制图形时将其过滤掉)使其唯一,即:

Note that set members have to be unique so if [result]'s cardinality is low, make it unique by concatenating the timestamp to it (and filtering it out when you graph), i.e.:

ZADD [system]:[event] [timestamp] [result]:[timestamp]

通过这种方式,您将能够通过调用 ZRANGEBYSCORE 并绘制结果来获取测量范围.

This way you'll be able to fetch ranges of measurements by calling ZRANGEBYSCORE and graphing the results.

这篇关于想使用Redis作为事件统计存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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