在 Redis 中过滤排行榜的类别 [英] Filtering on a category for leaderboards in Redis

查看:51
本文介绍了在 Redis 中过滤排行榜的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Redis 和排序集来为游戏创建排行榜,并设法让基本版本正常运行.我想知道是否可以进一步过滤类别?例如,假设我正在跟踪全球用户的分数,并且每个用户都有一个国家字段,用于指定他们来自哪里.

I'm using Redis and sorted sets to create a leaderboard for a game, and have managed to get a basic version working. I'm wondering if it's possible to further filter on categories? For instance, let's say I am tracking the scores of users globally, and each user has a country field designating where they are from.

是否可以进行过滤以从特定国家/地区(而不是整个数据库)中提取前 10 名用户?

Is it possible to do a filter where I want to pull out the top 10 users from a particular country (rather than the whole database)?

推荐答案

是的,这是可能的,但它会增加应用层复杂性和数据级存储来实现它.由于 Redis 是键/值存储 - 查询多个条件(国家、游戏级别、性别、每日/每周/每月顶级用户)表明您可能需要另一种解决方案来解决此问题.当您需要查询多个字段时,您会不断对数据进行非规范化,以满足业务需求.Redis 不是此类问题的最佳解决方案之一.

Yes it is possible but it will increase both application layer complexity and data level storage to accomplish it. Since Redis is a key/value store - querying on multiple conditions(country, game level, gender, daily/weekly/monthly top users) is a sign of you may need another solution for this problem. When you need to query multiple fields you keep denormalize your data to comfort the requirements of the business. Redis is not one of the best solutions for these kind of problems.

Cassandra(宽列存储,NoSQL 数据库)或 PostgreSQL(RDBM)将是这个阶段需要考虑的解决方案.您可以继续写入/更新 Postgres 并将结果缓存在 Redis 中一段时间​​.

Cassandra(wide column store, NoSQL database) or PostgreSQL(RDBMs) would be a solution to consider at this phase. You may keep writing/updating to Postgres and cache your results in Redis for a given time.

如果你想在Redis中实现这个;您需要使用国家/地区语言环境作为密钥的一部分.

If you want to implement this in Redis; You need to use country locales as part of the key.

您将拥有 users 键来跟踪全局用户,并且您将拥有 users:itusers:es 等键来跟踪用户根据他们的国家跟踪他们.每当您设置/更新用户时,您都需要在用户所在的国家/地区设置/更新它们.

You will have users key to track of users globally and you will have keys like users:it, users:es etc. to track them according to their country. Whenever you set/update the user you need to set/update them in the set of user's country.

127.0.0.1:6379> zadd users 15 a
(integer) 1
127.0.0.1:6379> zadd users:it 15 a
(integer) 1
127.0.0.1:6379> zadd users 23 b 34 c
(integer) 2
127.0.0.1:6379> zadd users:es 23 b 34 c
(integer) 2
127.0.0.1:6379> zrevrange users 0 -1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> zrevrange users 0 -1 withscores
1) "c"
2) "34"
3) "b"
4) "23"
5) "a"
6) "15"
127.0.0.1:6379> zrevrange users:it 0 -1 withscores
1) "a"
2) "15"
127.0.0.1:6379> zrevrange users:es 0 -1 withscores
1) "c"
2) "34"
3) "b"
4) "23"
127.0.0.1:6379> zadd users 45 b
(integer) 0
127.0.0.1:6379> zadd users:es 45 b
(integer) 0
127.0.0.1:6379> zrevrange users:es 0 -1 withscores
1) "b"
2) "45"
3) "c"
4) "34"
127.0.0.1:6379> zrevrange users 0 -1 withscores
1) "b"
2) "45"
3) "c"
4) "34"
5) "a"
6) "15"
127.0.0.1:6379>

这种解决方案的一个问题是,当您需要另一个标准来列出用户时,您需要将所有现有用户迁移到新系统,您的数据大小会显着增加.

One of the problems with that kind of solution is that when you need another criteria to list users, you need to migrate all of the existing users to the new system, your data size will increase significantly.

这篇关于在 Redis 中过滤排行榜的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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