我有两个已排序的集合,我想将记录与第一个集合分开,并通过从第二个已排序的集合中排除记录来存储在新的列表/排序集中。
下面是一个例子:
第 1 组:1,2,3,4,5 set2: 3,5,7,8,9
输出:1,2,4
编辑: 我已经找到加载脚本并使用 eval 从 nodejs 执行脚本的方法。
奇怪的是,当我执行你的脚本时,即使是 5-10 条记录,它也需要 1 秒来处理,这让我怀疑如果我有数千条记录,它的可扩展性有多大。
下面是我的示例 nodejs 代码:
hsetxx = 'redis.call("ZINTERSTORE","temp",2,"set11","set21","weights",1,0) redis.call("ZUNIONSTORE","result",2,"set11","temp","weights",1,-1) redis.call("ZREMRANGEBYSCORE","result",0,0)';
var redis = require('redis');
var client = redis.createClient('14470', connection);
client.on('connect', function() {
console.log('Connected to Redis');
});
client.script('load',hsetxx,function(err, result) {
console.log(err+'------------'+result);
});
client.zadd('set11', 1,1,1,2,1,3,1,4,1,5);
client.zadd('set21', 1,1,1,5);
client.evalsha(
'39c0da298cab6a6223b4d1e8222cf6d6a84e67b1', //lua source
0,
function(err, result) {
client.zrange('result', 0, -1, function(err, result) {
console.log(err+'------------'+result);
});
}
);
最佳答案
检查 this问题:
What you could do is first create a temporary set with ZUNIONSTORE and set the intersect's scores to 0. Then do a range excluding the 0, e.g.:
127.0.0.1:6379> ZADD all 1 one 2 two 3 three (integer) 3 127.0.0.1:6379> SADD disabled two (integer) 1 127.0.0.1:6379> ZUNIONSTORE tmp 2 all disabled WEIGHTS 1 0 AGGREGATE MIN (integer) 3 127.0.0.1:6379> ZREVRANGEBYSCORE tmp +inf 1 WITHSCORES 1) "three" 2) "3" 3) "one" 4) "1"
关于redis - 在 Redis 中,如何通过从第二个排序集中排除记录来将记录与第一个集分开。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41377402/