Redis 选择数据类型的建议 [英] Redis suggesstion for selecting data type

查看:54
本文介绍了Redis 选择数据类型的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有关于我们在主页中显示 2 个列表的位置的问题

We have questions based where in home page we were showing 2 list

  1. 按日期修改的问题
  2. 问题有更大的观点和答案.在这两个列表中,如果问题具有相同的观看次数或回答次数,则排序基于日期.

以前我直接查询 MySQL 数据库并获取值,因此很容易.但是每次访问 MySQL 的页面请求都会有点昂贵,然后开始缓存.

Previously i am directly quiring to MySQL database and fetching the values so it's easy. But each page request hitting to MySQL it's bit expensive then start doing caching.

我开始使用Redis.以下是我使用redis缓存的情况

I started using Redis. Following is the cases when i use redis cache

问题是在第二个列表中,我必须按投票显示问题,而不是合并回答.我如何将这种类型的数据存储在 redis 中以通过基于 2 个条件投票和时间计数的排序来更快地加载?

Issues is On second listing i have to display questions by votes and not answered combine. How can i stored this type of data in redis to load faster with sorting based by 2 conditions votes with time and ans count with time?

推荐答案

你可以在 redis 中使用排序集.您的查看或回答计数可以是分数.根据时间戳创建密钥.排序集合方法 zrevrangebyscore 会给你正确的顺序.

You can use sorted sets in redis. Your view or answer count can be the score. create a key based on timestamp. Sorted set method zrevrangebyscore will give you the correct order.

您可以将有序集合的成员设置为:

you can set your member of sorted set as:

'YEAR_MONTH_DATE_HOUR_MINUTE_SECONDS:question_id'

这样如果你排序,分数相同的问题将按字典顺序返回.这样一来,如果您使用 zrevrangebyscore,那么稍后出现的问题将被置于更高的位置.

This way if you sort, questions with same score, will be returned in lexicographical order. That way question which came later will be placed higher if you use zrevrangebyscore.

您可以创建一个哈希映射来映射时间戳和问题 ID.快速查找

You can create a hash map to map timestamp and question_id. for faster lookup

我问了一个类似的问题,我也在那里想了一个解决方案.我想要一些不同的东西,但它会做你想要的.

I asked a similar question, where I also purposed a solution. I want something different but it will do exactly what you want.

Redis zrevrangebyscore,非字典序排序

这篇关于Redis 选择数据类型的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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