class Redis::Set
Class representing a set.
Public Instance Methods
Works like add. Can chain together: list << ‘a’ << ‘b’
# File lib/redis/set.rb, line 9 def <<(value) add(value) self # for << 'a' << 'b' end
# File lib/redis/set.rb, line 167 def ==(x) members == x end
Add the specified value to the set only if it does not exist already. Redis: SADD
# File lib/redis/set.rb, line 16 def add(value) allow_expiration do redis.sadd(key, marshal(value)) if value.nil? || !Array(value).empty? end end
Delete the value from the set. Redis: SREM
# File lib/redis/set.rb, line 55 def delete(value) redis.srem(key, marshal(value)) end
Delete if matches block
# File lib/redis/set.rb, line 60 def delete_if(&block) res = false redis.smembers(key).each do |m| if block.call(unmarshal(m)) res = redis.srem(key, m) end end res end
Return the difference vs another set. Can pass it either another set object or set name. Also available as ^ or - which is a bit cleaner:
members_difference = set1 ^ set2 members_difference = set1 - set2
If you want to specify multiple sets, you must use difference
:
members_difference = set1.difference(set2, set3, set4) members_difference = set1.diff(set2, set3, set4)
Redis: SDIFF
# File lib/redis/set.rb, line 129 def difference(*sets) redis.sdiff(key, *keys_from_objects(sets)).map{|v| unmarshal(v)} end
Calculate the diff and store it in Redis
as name
. Returns the number of elements in the stored union. Redis: SDIFFSTORE
# File lib/redis/set.rb, line 138 def diffstore(name, *sets) redis.sdiffstore(name, key, *keys_from_objects(sets)) end
Returns true if the set has no members. Redis: SCARD == 0
# File lib/redis/set.rb, line 163 def empty? length == 0 end
Return the intersection with another set. Can pass it either another set object or set name. Also available as & which is a bit cleaner:
members_in_both = set1 & set2
If you want to specify multiple sets, you must use intersection
:
members_in_all = set1.intersection(set2, set3, set4) members_in_all = set1.inter(set2, set3, set4) # alias
Redis: SINTER
# File lib/redis/set.rb, line 81 def intersection(*sets) redis.sinter(key, *keys_from_objects(sets)).map{|v| unmarshal(v)} end
Calculate the intersection and store it in Redis
as name
. Returns the number of elements in the stored intersection. Redis: SUNIONSTORE
# File lib/redis/set.rb, line 90 def interstore(name, *sets) redis.sinterstore(name, key, *keys_from_objects(sets)) end
The number of members in the set. Aliased as size or count. Redis: SCARD
# File lib/redis/set.rb, line 156 def length redis.scard(key) end
Returns true if the specified value is in the set. Redis: SISMEMBER
# File lib/redis/set.rb, line 49 def member?(value) redis.sismember(key, marshal(value)) end
Return all members in the set. Redis: SMEMBERS
# File lib/redis/set.rb, line 41 def members vals = redis.smembers(key) vals.nil? ? [] : vals.map{|v| unmarshal(v) } end
Adds the specified values to the set. Only works on redis > 2.4 Redis: SADD
# File lib/redis/set.rb, line 34 def merge(*values) allow_expiration do redis.sadd(key, values.flatten.map{|v| marshal(v)}) end end
Moves value from one set to another. Destination can be a String or Redis::Set
.
set.move(value, "name_of_key_in_redis") set.move(value, set2)
Returns true if moved successfully.
Redis: SMOVE
# File lib/redis/set.rb, line 151 def move(value, destination) redis.smove(key, destination.is_a?(Redis::Set) ? destination.key : destination.to_s, value) end
Remove and return a random member. Redis: SPOP
# File lib/redis/set.rb, line 23 def pop(count = nil) unmarshal redis.spop(key, count) end
return a random member. Redis: SRANDMEMBER
# File lib/redis/set.rb, line 28 def randmember(count = nil) unmarshal redis.srandmember(key, count) end
# File lib/redis/set.rb, line 171 def to_s members.join(', ') end
Return the union with another set. Can pass it either another set object or set name. Also available as | and + which are a bit cleaner:
members_in_either = set1 | set2 members_in_either = set1 + set2
If you want to specify multiple sets, you must use union
:
members_in_all = set1.union(set2, set3, set4)
Redis: SUNION
# File lib/redis/set.rb, line 105 def union(*sets) redis.sunion(key, *keys_from_objects(sets)).map{|v| unmarshal(v)} end
Calculate the union and store it in Redis
as name
. Returns the number of elements in the stored union. Redis: SUNIONSTORE
# File lib/redis/set.rb, line 113 def unionstore(name, *sets) redis.sunionstore(name, key, *keys_from_objects(sets)) end
Private Instance Methods
# File lib/redis/set.rb, line 177 def keys_from_objects(sets) raise ArgumentError, "Must pass in one or more set names" if sets.empty? sets.collect{|set| set.is_a?(Redis::Set) ? set.key : set} end