【问题标题】:Redis structure to store popular articles by categoryRedis 结构按类别存储热门文章
【发布时间】:2014-06-25 16:05:05
【问题描述】:

我正在尝试找出一种方法,以便在 redis 中按类别有效地存储和检索文章的受欢迎程度。目前我正在考虑这样的解决方案。

创建一堆散列来跟踪文章在所有类别中的受欢迎程度,其中键是“全部”、年、月或周初,字段是文章 ID,值是计数器。为了更新一篇文章的受欢迎程度,我将使用 HINCRBY 来增加该文章的计数器

整体流行度的哈希值:

all: article_id <counter>  // all time popular
2012: article_id <counter> // popular by year
2012-01: article_id <counter>  // popular by month
2012-01-04: article_id <counter>    // popular by week, where the date is the beginning of the week

并为每个类别创建一组哈希,例如下面是“category_1”的哈希

<category_1>:all: article_id <counter>  // all time popular
<category_1>:2012: article_id <counter> // popular by year
<category_1>:2012-01: article_id <counter>  // popular by month
<category_1>:2012-01-04: article_id <counter>   // popular by week, where the date is the beginning of the week

'category_2' 的另一组

<category_2>:all: article_id <counter>  // all time popular
<category_2>:2012: article_id <counter> // popular by year
<category_2>:2012-01: article_id <counter>  // popular by month
<category_2>:2012-01-04: article_id <counter>   // popular by week, where the date is the beginning of the week 

所以每当一篇文章的受欢迎程度上升时,我都会增加两组哈希值,一组用于整体,另一组用于文章所属的类别。我还没有弄清楚如何检索最流行的文章(所有时间、每年等),甚至不确定是否可以使用“哈希”数据类型。

散列是正确的数据结构吗?关于如何为此建模解决方案的任何想法都会有所帮助。

【问题讨论】:

    标签: redis


    【解决方案1】:

    我认为您考虑使用排序集而不是哈希。基本上,使用 article_id 作为成员,使用流行度作为分数。为每个时间分辨率和类别排列保留一个排序集 - 就像你用哈希描述的一样。这将允许您使用简单的ZRANGEBYSCORE 按受欢迎程度(分数)获取文章(集合成员)。要更新人气,请发送ZINCRBY

    【讨论】:

    • 感谢您使用您的解决方案已经有一段时间了。快速提问,我可以使用 ZINCRBY,而不是进行 ZSCORE 检查,如果 article_id 不存在,它会自动添加,如果存在则增加分数。你觉得 ZINCRBY 有什么问题吗?
    • 你当然是对的 - 完全忘记了 ZINCRBY :) 编辑答案以反映这一点。
    猜你喜欢
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    相关资源
    最近更新 更多