Redis 的“分数"用例和“排名"集合的特征 [英] Use Cases for Redis' "Score" and "Ranking" Features for Sets

查看:70
本文介绍了Redis 的“分数"用例和“排名"集合的特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Redis 的得分"和排名"功能有哪些用例(在游戏的典型排行榜"示例之外?我正在尝试弄清楚如何利用这些动态新功能作为我预计会从使用传统关系数据库转向使用 Redis 作为持久数据存储.

What are some use cases for Redis' "score" and "ranking" features for sets (outside of the typical "leaderboard" examples for games? I'm trying to figure out how to make use of these dynamic new features as I anticipate moving from using a traditional relational database to Redis as a persistent data store.

推荐答案

ZSET 非常适合基于分数的选择或范围,但分数可以是任何数值,例如时间戳.

ZSETs are great for selections or ranges based on scores, but scores can be any numerical value, like a timestamp.

我们将所有美国股票的每日股票价格存储在 redis 中.这是 eBay 的示例...

We store daily stock prices for all US stocks in redis. Here's an example for ebay...

ZADD key score member [score member ...] 
...
ZADD stocks:ebay 1 30.39 2 32.70 3 31.25 4 31.75 5 29.12 6 29.87 7 29.93

在这种情况下,score 值通常是长时间戳,除此之外,如果我们想要过去 3 天的每日价格,我们只需将两个日期转换为时间戳并使用时间戳范围 1 3...

The score values in this case would normally be long timestamps, with that aside, if we want daily prices for the last 3 days, we simply convert two dates to timestamps and pull from redis using the timestamp range 1 3...

zrangebyscore stocks:ebay 1 3

1) "30.39"
2) "32.70"
3) "31.25"

查询速度非常并且非常适合我们的需求.

The query is very fast and works well for our needs.

希望有帮助!

这篇关于Redis 的“分数"用例和“排名"集合的特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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