小编典典

Redis结构可按类别存储热门文章

redis

我正在尝试找出一种方法,可以随着时间的推移在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

因此,每当文章的知名度上升时,我都会增加两组哈希,一组用于总体,另一组用于该文章所属的类别。我还没有弄清楚如何检索最受欢迎的文章(始终,每年等),甚至不确定是否可以使用“哈希”数据类型。

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


阅读 366

收藏
2020-06-20

共1个答案

小编典典

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

2020-06-20