Class: Redis::Set

Inherits:
BaseObject show all
Includes:
Enumerable, Helpers::CoreCommands
Defined in:
lib/redis/set.rb

Overview

Class representing a set.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CoreCommands

#exists?, #expire, #expireat, #marshal, #persist, #rename, #renamenx, #sort, #ttl, #type, #unmarshal

Methods inherited from BaseObject

expiration_filter, #initialize, #redis, #set_expiration

Constructor Details

This class inherits a constructor from Redis::BaseObject

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key



13
14
15
# File 'lib/redis/set.rb', line 13

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options



13
14
15
# File 'lib/redis/set.rb', line 13

def options
  @options
end

Instance Method Details

#<<(value) ⇒ Object

Works like add. Can chain together: list << 'a' << 'b'



16
17
18
19
# File 'lib/redis/set.rb', line 16

def <<(value)
  add(value)
  self  # for << 'a' << 'b'
end

#==(x) ⇒ Object



175
176
177
# File 'lib/redis/set.rb', line 175

def ==(x)
  members == x
end

#add(value) ⇒ Object

Add the specified value to the set only if it does not exist already. Redis: SADD



23
24
25
# File 'lib/redis/set.rb', line 23

def add(value)
  redis.sadd(key, marshal(value)) if value.nil? || !Array(value).empty?
end

#delete(value) ⇒ Object

Delete the value from the set. Redis: SREM



57
58
59
# File 'lib/redis/set.rb', line 57

def delete(value)
  redis.srem(key, marshal(value))
end

#delete_if(&block) ⇒ Object

Delete if matches block



62
63
64
65
66
67
68
69
70
# File 'lib/redis/set.rb', line 62

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



137
138
139
# File 'lib/redis/set.rb', line 137

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



146
147
148
# File 'lib/redis/set.rb', line 146

def diffstore(name, *sets)
  redis.sdiffstore(name, key, *keys_from_objects(sets))
end

#each(&block) ⇒ Object

Iterate through each member of the set. Redis::Objects mixes in Enumerable, so you can also use familiar methods like collect, detect, and so forth.



74
75
76
# File 'lib/redis/set.rb', line 74

def each(&block)
  members.each(&block)
end

#empty?Boolean

Returns true if the set has no members. Redis: SCARD == 0

Returns:

  • (Boolean)


171
172
173
# File 'lib/redis/set.rb', line 171

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



89
90
91
# File 'lib/redis/set.rb', line 89

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



98
99
100
# File 'lib/redis/set.rb', line 98

def interstore(name, *sets)
  redis.sinterstore(name, key, *keys_from_objects(sets))
end

#lengthObject Also known as: size, count

The number of members in the set. Aliased as size. Redis: SCARD



164
165
166
# File 'lib/redis/set.rb', line 164

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

Returns:

  • (Boolean)


51
52
53
# File 'lib/redis/set.rb', line 51

def member?(value)
  redis.sismember(key, marshal(value))
end

#membersObject Also known as: get

Return all members in the set. Redis: SMEMBERS



44
45
46
47
# File 'lib/redis/set.rb', line 44

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



39
40
41
# File 'lib/redis/set.rb', line 39

def merge(*values)
  redis.sadd(key, values.flatten.map{|v| marshal(v)})
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



159
160
161
# File 'lib/redis/set.rb', line 159

def move(value, destination)
  redis.smove(key, destination.is_a?(Redis::Set) ? destination.key : destination.to_s, value)
end

#popObject

Remove and return a random member. Redis: SPOP



28
29
30
# File 'lib/redis/set.rb', line 28

def pop
  unmarshal redis.spop(key)
end

#randmember(count = nil) ⇒ Object

return a random member. Redis: SRANDMEMBER



33
34
35
# File 'lib/redis/set.rb', line 33

def randmember(count = nil)
  unmarshal redis.srandmember(key, count)
end

#to_sObject



179
180
181
# File 'lib/redis/set.rb', line 179

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



113
114
115
# File 'lib/redis/set.rb', line 113

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



121
122
123
# File 'lib/redis/set.rb', line 121

def unionstore(name, *sets)
  redis.sunionstore(name, key, *keys_from_objects(sets))
end