redis - 在 Redis 中执行 sunion 时获取计数的方法

标签 redis

我正在使用 Redis 进行评估,但有一个用例非常出色。我想使用 SUNION,但也想找回计数。就像目前来自他们的文档的 SUNION http://redis.io/commands/sunion :

key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SUNION key1 key2 key3 = {a,b,c,d,e}

但是想要:

SOTHERUNION key1 key2 key3 = {a:2,b:1,c:3,d:1,e:1}

理想情况下排序如下:

SOTHERUNION key1 key2 key3 = {c:3, a:2,b:1,d:1,e:1}

这可能吗(尤其是以高性能的方式)?我们在 MySQL 中执行此操作,可能会出现问题。

最佳答案

SUNION不会在这里帮助你,但ZUNIONSTORE是你要找的。尽管它的名称如此,它也适用于常规集(默认情况下为成员提供 1 分)并与 AGGREGATE SUM 子句结合使用,它将产生请求的结果:

127.0.0.1:6379> sadd key1 a b c d
(integer) 4
127.0.0.1:6379> sadd key2 c
(integer) 1
127.0.0.1:6379> sadd key3 a c e
(integer) 3
127.0.0.1:6379> zunionstore result 3 key1 key2 key3 aggregate sum
(integer) 5
127.0.0.1:6379> zrevrangebyscore result +inf -inf
1) "c"
2) "a"
3) "e"
4) "d"
5) "b"
127.0.0.1:6379> zscore result c
"3"
127.0.0.1:6379> zscore result a
"2"
127.0.0.1:6379> zscore result e
"1"
127.0.0.1:6379> zscore result d
"1"
127.0.0.1:6379> zscore result b
"1"

关于redis - 在 Redis 中执行 sunion 时获取计数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007782/

相关文章:

python - 向 python-rom 对象添加过期功能

node.js - “工作完成”事件未在 Kue 中触发

php - 脚本/队列不断耗尽内存

编译执行Redis ziplist.c

Redis 对二级索引的支持

lua - 在redis中存储和使用lua脚本

java - 在spring cache中通过方法访问返回数据

php - 使用 Redis DB 的 Php 分页

rest - Rest api中的redis key 更新通知

php - Laravel 5.6升级后Redis "Error while reading line from the server."