按类别存储热门文章的Redis结构 [英] Redis structure to store popular articles by category

查看:37
本文介绍了按类别存储热门文章的Redis结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to figure out a way to efficiently store and retrieve the popularity of articles by category over time in redis. For now I am thinking of a solution like this.

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

Create a bunch of hashes to track the popularity of an article across all categories, where the key is 'all', year, month or begining of week and field is the article id with value being the counter. To update the popularity of an article I'll use the HINCRBY to increment the counter for that article

整体流行度的哈希:

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"的哈希值

And create set of Hashes for every category, for example below are the hashes for '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"的另一组

Another set for '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 

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

So everytime the popularity of an articles goes up, I'll increment two sets of hashes, one for overall and the other for category the article belongs to. I am yet to figure out how to retrieve the most popular articles ( alltime, yearly, etc ) and not even sure if it will be possible using the 'hashes' data type.

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

Is hashes the correct datastructure for this ? Any thoughts on how to model a solution for this will helpful.

推荐答案

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

I think you look into using sorted sets instead of hashes. Basically, use the article_id as the member and popularity as score. Keep a sorted set for each time resolution & category permutation - just like what you've described with hashes. This will allow you to fetch articles (set members) by popularity (score) with a simple ZRANGEBYSCORE. To update the popularity, do a ZINCRBY.

这篇关于按类别存储热门文章的Redis结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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