redis 从集合中删除特定成员

Gag*_*gan 4 set members redis

我有一组成员。例如,名为“college”的集合,其中包含 20 个学院的名称。

现在,如何从集合中仅删除一个子集,例如选定的 10 所大学?

我正在运行 v2.4.5 的 redis 服务器

此处找到的文档http://redis.io/commands/srem说我们可以删除 redis >= 2.4 的多个键,但仍然不知道如何实现这一点。

我正在使用 RubyonRails,并且是在 Rails 控制台中完成的

> $redis
    #<Redis client v2.2.2 connected to redis://localhost:6379/0 (Redis v2.4.5)>
> ruby-1.9.3-p0 :011 > $redis.sadd("college","champion1")
 => true 
ruby-1.9.3-p0 :012 > $redis.sadd("college","champion2")
 => true 
ruby-1.9.3-p0 :013 > $redis.sadd("college","champion3")
 => true 
ruby-1.9.3-p0 :014 > $redis.sadd("college","champion4")
 => true 
ruby-1.9.3-p0 :015 > $redis.sadd("college","champion5")
 => true
ruby-1.9.3-p0 :016 > $redis.smembers("college")
 => ["champion1", "champion2", "champion3", "champion4", "champion5"
ruby-1.9.3-p0 :017 > $redis.srem("college","champion1" "champion2")
 => false
ruby-1.9.3-p0 :018 > $redis.smembers("college")
 => ["champion1", "champion2", "champion3", "champion4", "champion5"]
Run Code Online (Sandbox Code Playgroud)

成员“champion1”和“champion2”不会从集合中删除。

我已经安装了 redis (2.2.2 ruby​​) gem。

Gag*_*gan 6

抱歉各位,这是 Rails 特有的问题。

我已经安装了redis gem 2.2.2版本,它不支持删除多个键。但是,无论如何进入 redis-cli for redis server >=2.4 我们都可以实现它。

gagan@gagan-desktop:~/projects/test_proj [master]$ redis-cli
redis 127.0.0.1:6379> smembers "college"
1) "champion1"
2) "champion2"
3) "champion3"
4) "champion4"
5) "champion5"
redis 127.0.0.1:6379> srem "college" "champion1" "champion2"
(integer) 2
redis 127.0.0.1:6379> smembers "college"
1) "champion3"
2) "champion4"
3) "champion5"
redis 127.0.0.1:6379> exit
Run Code Online (Sandbox Code Playgroud)