gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:15:02 25 4
gpt4 key购买 nike

我正在尝试找出一种方法来在 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

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

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

最佳答案

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

关于Redis结构按类别存储热门文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24378070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com