Redis suggesstion for selecting data type

2019-04-17 12:07发布

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

  1. Questions by date modified
  2. Question have bigger views and ans count. And in this both listing if question have same views or ans count then sorting is based on date.

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.

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

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
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-17 12:45

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'

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.

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, sorting other than lexicographical order

查看更多
登录 后发表回答