本文介绍了redis关键分析方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用redis创建分析 - 每个对象的基本计数器,每小时/每天/每周/每月/每年和总计

什么redis数据结构会有效为什么我可以避免对redis进行多次调用?

最好让每个模型都有这组键:



散列 - 模型:< id>:年份 =>每年都有一个计数器



hash - model:< id>:< year> =>每个月都有一个计数器

hash - 型号:< id>:< year>:<月份>每天有一个计数器

hash - model:< id>:< year>:< month>:< day> =>每小时都有一个计数器



如果这个方案是正确的,我如何绘制这些数据而不需要对redis进行多次调用?我必须在模型全年循环:< id>:years 并获取月份,然后在月份中循环等等。或者我只是将所有键的所有字段和值作为一个批量请求,然后在服务器中处理?

解决方案

最好使用来替代散列。 使用 timestamp 作为分数,您将能够检索特定时间范围内的数据

对于日期范围,您可以使用模型:< id>:< year>:<月> ,在一小时范围内(使用模型:< id>:< year>:<月份>:< day> )等等......

,如果日期范围大于一个月(例如2014年1月1日至2014年3月20日),则必须检索多个zset(型号:< id>:2014:01 model:< id>:2014:02 model:< id>:2014:03 )并合并结果。

如果您真的想在单个请求中执行日期范围,则可以将日精度数据始终存储在模型:其中ID>:其中年> 。如果你想处理多年的日期范围,你只需要一个zset,例如 model:< id>:byDay

然而,请注意,存储历史数据会增加内存消耗,因此您应该已经考虑过 数据保留 一>。使用Redis,你可以在zset上使用 EXPIRE 或者用cron自己做。


I want to create analytics using redis - basic counters per object, per hour/day/week/month/year and total

what redis data structure would be effective for this and how can I avoid doing many calls to redis?

would it better to have each model have this sets of keys:

hash - model:<id>:years => every year has a counter

hash - model:<id>:<year> => every month has a counter

hash - model:<id>:<year>:<month> => every day has a counter

hash - model:<id>:<year>:<month>:<day> => every hour has a counter

if this scheme is correct, how would I chart this data without doing many calls to redis? I would have to loop on all year in model:<id>:years and fetch the month, then loop on the month, etc? Or I just grab all fields and their values from all keys as a batch request and then process that in the server?

解决方案

It's better to use a zset for this instead of an hash. Using timestamp as score you will be able to retrieve data for specific time range

For a date range you will use model:<id>:<year>:<month>, for an hour range (using model:<id>:<year>:<month>:<day>) and so on...

Indeed, if the date range is larger than a month (e.g. from January 1st 2014 to March 20th 2014), you will have to retrieve multiple zset (model:<id>:2014:01, model:<id>:2014:02 and model:<id>:2014:03) and merge the results.

If you really want to do a date range inside a single request, you can always store day precision data inside model:<id>:<year>. And if you want to handle date range over multiple years, you will just need to have a single zset e.g. model:<id>:byDay.

However, please note that storing historical data will increase memory consumption over time so you should already think about data retention. With Redis you can either use EXPIRE on zset or do it yourself with crons.

这篇关于redis关键分析方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:50