Class: Redis::Set
- Inherits:
-
EnumerableObject
- Object
- BaseObject
- EnumerableObject
- Redis::Set
- Defined in:
- lib/redis/set.rb
Overview
Class representing a set.
Instance Attribute Summary
Attributes inherited from BaseObject
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Works like add.
- #==(x) ⇒ Object
-
#add(value) ⇒ Object
Add the specified value to the set only if it does not exist already.
-
#delete(value) ⇒ Object
Delete the value from the set.
-
#delete_if(&block) ⇒ Object
Delete if matches block.
-
#difference(*sets) ⇒ Object
(also: #diff, #^, #-)
Return the difference vs another set.
-
#diffstore(name, *sets) ⇒ Object
Calculate the diff and store it in Redis as
name. -
#empty? ⇒ Boolean
Returns true if the set has no members.
-
#intersection(*sets) ⇒ Object
(also: #intersect, #inter, #&)
Return the intersection with another set.
-
#interstore(name, *sets) ⇒ Object
Calculate the intersection and store it in Redis as
name. -
#length ⇒ Object
(also: #size, #count)
The number of members in the set.
-
#member?(value) ⇒ Boolean
(also: #include?)
Returns true if the specified value is in the set.
-
#members ⇒ Object
(also: #get, #value)
Return all members in the set.
-
#merge(*values) ⇒ Object
Adds the specified values to the set.
-
#move(value, destination) ⇒ Object
Moves value from one set to another.
-
#pop(count = nil) ⇒ Object
Remove and return a random member.
-
#randmember(count = nil) ⇒ Object
return a random member.
- #to_s ⇒ Object
-
#union(*sets) ⇒ Object
(also: #|, #+)
Return the union with another set.
-
#unionstore(name, *sets) ⇒ Object
Calculate the union and store it in Redis as
name.
Methods inherited from EnumerableObject
Methods inherited from BaseObject
#allow_expiration, #as_json, #initialize, #redis, #set_expiration, #to_hash, #to_json
Methods included from Helpers::CoreCommands
#exists, #exists?, #expire, #expireat, #marshal, #persist, #rename, #renamenx, #serializer, #ttl, #type, #unmarshal
Constructor Details
This class inherits a constructor from Redis::BaseObject
Instance Method Details
#<<(value) ⇒ Object
Works like add. Can chain together: list << ‘a’ << ‘b’
9 10 11 12 |
# File 'lib/redis/set.rb', line 9 def <<(value) add(value) self # for << 'a' << 'b' end |
#==(x) ⇒ Object
168 169 170 |
# File 'lib/redis/set.rb', line 168 def ==(x) members == x end |
#add(value) ⇒ Object
Add the specified value to the set only if it does not exist already. Redis: SADD
16 17 18 19 20 21 |
# File 'lib/redis/set.rb', line 16 def add(value) allow_expiration do value = '' if value.nil? redis.sadd(key, marshal(value)) if !Array(value).empty? # allow empty adds end end |
#delete(value) ⇒ Object
Delete the value from the set. Redis: SREM
56 57 58 |
# File 'lib/redis/set.rb', line 56 def delete(value) redis.srem(key, marshal(value)) end |
#delete_if(&block) ⇒ Object
Delete if matches block
61 62 63 64 65 66 67 68 69 |
# File 'lib/redis/set.rb', line 61 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 |
#difference(*sets) ⇒ Object Also known as: diff, ^, -
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
130 131 132 |
# File 'lib/redis/set.rb', line 130 def difference(*sets) redis.sdiff(key, *keys_from_objects(sets)).map{|v| unmarshal(v)} end |
#diffstore(name, *sets) ⇒ Object
Calculate the diff and store it in Redis as name. Returns the number of elements in the stored union. Redis: SDIFFSTORE
139 140 141 |
# File 'lib/redis/set.rb', line 139 def diffstore(name, *sets) redis.sdiffstore(name, key, *keys_from_objects(sets)) end |
#empty? ⇒ Boolean
Returns true if the set has no members. Redis: SCARD == 0
164 165 166 |
# File 'lib/redis/set.rb', line 164 def empty? length == 0 end |
#intersection(*sets) ⇒ Object Also known as: intersect, inter, &
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
82 83 84 |
# File 'lib/redis/set.rb', line 82 def intersection(*sets) redis.sinter(key, *keys_from_objects(sets)).map{|v| unmarshal(v)} end |
#interstore(name, *sets) ⇒ Object
Calculate the intersection and store it in Redis as name. Returns the number of elements in the stored intersection. Redis: SUNIONSTORE
91 92 93 |
# File 'lib/redis/set.rb', line 91 def interstore(name, *sets) redis.sinterstore(name, key, *keys_from_objects(sets)) end |
#length ⇒ Object Also known as: size, count
The number of members in the set. Aliased as size or count. Redis: SCARD
157 158 159 |
# File 'lib/redis/set.rb', line 157 def length redis.scard(key) end |
#member?(value) ⇒ Boolean Also known as: include?
Returns true if the specified value is in the set. Redis: SISMEMBER
50 51 52 |
# File 'lib/redis/set.rb', line 50 def member?(value) redis.sismember(key, marshal(value)) end |
#members ⇒ Object Also known as: get, value
Return all members in the set. Redis: SMEMBERS
42 43 44 45 |
# File 'lib/redis/set.rb', line 42 def members vals = redis.smembers(key) vals.nil? ? [] : vals.map{|v| unmarshal(v) } end |
#merge(*values) ⇒ Object
Adds the specified values to the set. Only works on redis > 2.4 Redis: SADD
35 36 37 38 39 |
# File 'lib/redis/set.rb', line 35 def merge(*values) allow_expiration do redis.sadd(key, values.flatten.map{|v| marshal(v)}) end end |
#move(value, destination) ⇒ Object
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
152 153 154 |
# File 'lib/redis/set.rb', line 152 def move(value, destination) redis.smove(key, destination.is_a?(Redis::Set) ? destination.key : destination.to_s, value) end |
#pop(count = nil) ⇒ Object
Remove and return a random member. Redis: SPOP
24 25 26 |
# File 'lib/redis/set.rb', line 24 def pop(count = nil) unmarshal redis.spop(key, count) end |
#randmember(count = nil) ⇒ Object
return a random member. Redis: SRANDMEMBER
29 30 31 |
# File 'lib/redis/set.rb', line 29 def randmember(count = nil) unmarshal redis.srandmember(key, count) end |
#to_s ⇒ Object
172 173 174 |
# File 'lib/redis/set.rb', line 172 def to_s members.join(', ') end |
#union(*sets) ⇒ Object Also known as: |, +
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
106 107 108 |
# File 'lib/redis/set.rb', line 106 def union(*sets) redis.sunion(key, *keys_from_objects(sets)).map{|v| unmarshal(v)} end |
#unionstore(name, *sets) ⇒ Object
Calculate the union and store it in Redis as name. Returns the number of elements in the stored union. Redis: SUNIONSTORE
114 115 116 |
# File 'lib/redis/set.rb', line 114 def unionstore(name, *sets) redis.sunionstore(name, key, *keys_from_objects(sets)) end |