Module: Redis::Commands::Sets

Included in:
Redis::Commands
Defined in:
lib/redis/commands/sets.rb

Instance Method Summary collapse

Instance Method Details

#sadd(key, *members) ⇒ Integer

Add one or more members to a set.

Parameters:

  • key (String)
  • member (String, Array<String>)

    one member, or array of members

Returns:

  • (Integer)

    The number of members that were successfully added



19
20
21
22
# File 'lib/redis/commands/sets.rb', line 19

def sadd(key, *members)
  members.flatten!(1)
  send_command([:sadd, key].concat(members))
end

#sadd?(key, *members) ⇒ Boolean

Add one or more members to a set.

Parameters:

  • key (String)
  • member (String, Array<String>)

    one member, or array of members

Returns:

  • (Boolean)

    Wether at least one member was successfully added.



29
30
31
32
# File 'lib/redis/commands/sets.rb', line 29

def sadd?(key, *members)
  members.flatten!(1)
  send_command([:sadd, key].concat(members), &Boolify)
end

#scard(key) ⇒ Integer

Get the number of members in a set.

Parameters:

  • key (String)

Returns:

  • (Integer)


10
11
12
# File 'lib/redis/commands/sets.rb', line 10

def scard(key)
  send_command([:scard, key])
end

#sdiff(*keys) ⇒ Array<String>

Subtract multiple sets.

Parameters:

  • keys (String, Array<String>)

    keys pointing to sets to subtract

Returns:

  • (Array<String>)

    members in the difference



123
124
125
126
# File 'lib/redis/commands/sets.rb', line 123

def sdiff(*keys)
  keys.flatten!(1)
  send_command([:sdiff].concat(keys))
end

#sdiffstore(destination, *keys) ⇒ Integer

Subtract multiple sets and store the resulting set in a key.

Parameters:

  • destination (String)

    destination key

  • keys (String, Array<String>)

    keys pointing to sets to subtract

Returns:

  • (Integer)

    number of elements in the resulting set



133
134
135
136
# File 'lib/redis/commands/sets.rb', line 133

def sdiffstore(destination, *keys)
  keys.flatten!(1)
  send_command([:sdiffstore, destination].concat(keys))
end

#sinter(*keys) ⇒ Array<String>

Intersect multiple sets.

Parameters:

  • keys (String, Array<String>)

    keys pointing to sets to intersect

Returns:

  • (Array<String>)

    members in the intersection



142
143
144
145
# File 'lib/redis/commands/sets.rb', line 142

def sinter(*keys)
  keys.flatten!(1)
  send_command([:sinter].concat(keys))
end

#sinterstore(destination, *keys) ⇒ Integer

Intersect multiple sets and store the resulting set in a key.

Parameters:

  • destination (String)

    destination key

  • keys (String, Array<String>)

    keys pointing to sets to intersect

Returns:

  • (Integer)

    number of elements in the resulting set



152
153
154
155
# File 'lib/redis/commands/sets.rb', line 152

def sinterstore(destination, *keys)
  keys.flatten!(1)
  send_command([:sinterstore, destination].concat(keys))
end

#sismember(key, member) ⇒ Boolean

Determine if a given value is a member of a set.

Parameters:

  • key (String)
  • member (String)

Returns:

  • (Boolean)


95
96
97
# File 'lib/redis/commands/sets.rb', line 95

def sismember(key, member)
  send_command([:sismember, key, member], &Boolify)
end

#smembers(key) ⇒ Array<String>

Get all the members in a set.

Parameters:

  • key (String)

Returns:

  • (Array<String>)


115
116
117
# File 'lib/redis/commands/sets.rb', line 115

def smembers(key)
  send_command([:smembers, key])
end

#smismember(key, *members) ⇒ Array<Boolean>

Determine if multiple values are members of a set.

Parameters:

  • key (String)
  • members (String, Array<String>)

Returns:

  • (Array<Boolean>)


104
105
106
107
108
109
# File 'lib/redis/commands/sets.rb', line 104

def smismember(key, *members)
  members.flatten!(1)
  send_command([:smismember, key].concat(members)) do |reply|
    reply.map(&Boolify)
  end
end

#smove(source, destination, member) ⇒ Boolean

Move a member from one set to another.

Parameters:

  • source (String)

    source key

  • destination (String)

    destination key

  • member (String)

    member to move from ‘source` to `destination`

Returns:

  • (Boolean)


86
87
88
# File 'lib/redis/commands/sets.rb', line 86

def smove(source, destination, member)
  send_command([:smove, source, destination, member], &Boolify)
end

#spop(key, count = nil) ⇒ String

Remove and return one or more random member from a set.

Parameters:

  • key (String)
  • count (Integer) (defaults to: nil)

Returns:

  • (String)


59
60
61
62
63
64
65
# File 'lib/redis/commands/sets.rb', line 59

def spop(key, count = nil)
  if count.nil?
    send_command([:spop, key])
  else
    send_command([:spop, key, Integer(count)])
  end
end

#srandmember(key, count = nil) ⇒ String

Get one or more random members from a set.

Parameters:

  • key (String)
  • count (Integer) (defaults to: nil)

Returns:

  • (String)


72
73
74
75
76
77
78
# File 'lib/redis/commands/sets.rb', line 72

def srandmember(key, count = nil)
  if count.nil?
    send_command([:srandmember, key])
  else
    send_command([:srandmember, key, count])
  end
end

#srem(key, *members) ⇒ Integer

Remove one or more members from a set.

Parameters:

  • key (String)
  • member (String, Array<String>)

    one member, or array of members

Returns:

  • (Integer)

    The number of members that were successfully removed



39
40
41
42
# File 'lib/redis/commands/sets.rb', line 39

def srem(key, *members)
  members.flatten!(1)
  send_command([:srem, key].concat(members))
end

#srem?(key, *members) ⇒ Boolean

Remove one or more members from a set.

Parameters:

  • key (String)
  • member (String, Array<String>)

    one member, or array of members

Returns:

  • (Boolean)

    Wether at least one member was successfully removed.



49
50
51
52
# File 'lib/redis/commands/sets.rb', line 49

def srem?(key, *members)
  members.flatten!(1)
  send_command([:srem, key].concat(members), &Boolify)
end

#sscan(key, cursor, **options) ⇒ String+

Scan a set

See the [Redis Server SSCAN documentation](redis.io/docs/latest/commands/sscan/) for further details

Examples:

Retrieve the first batch of keys in a set

redis.sscan("set", 0)

Parameters:

  • cursor (String, Integer)

    the cursor of the iteration

  • options (Hash)
    • ‘:match => String`: only return keys matching the pattern

    • ‘:count => Integer`: return count keys at most per iteration

Returns:

  • (String, Array<String>)

    the next cursor and all found members



189
190
191
# File 'lib/redis/commands/sets.rb', line 189

def sscan(key, cursor, **options)
  _scan(:sscan, cursor, [key], **options)
end

#sscan_each(key, **options, &block) ⇒ Enumerator

Scan a set

See the [Redis Server SSCAN documentation](redis.io/docs/latest/commands/sscan/) for further details

Examples:

Retrieve all of the keys in a set

redis.sscan_each("set").to_a
# => ["key1", "key2", "key3"]

Parameters:

  • options (Hash)
    • ‘:match => String`: only return keys matching the pattern

    • ‘:count => Integer`: return count keys at most per iteration

Returns:

  • (Enumerator)

    an enumerator for all keys in the set



206
207
208
209
210
211
212
213
214
215
# File 'lib/redis/commands/sets.rb', line 206

def sscan_each(key, **options, &block)
  return to_enum(:sscan_each, key, **options) unless block_given?

  cursor = 0
  loop do
    cursor, keys = sscan(key, cursor, **options)
    keys.each(&block)
    break if cursor == "0"
  end
end

#sunion(*keys) ⇒ Array<String>

Add multiple sets.

Parameters:

  • keys (String, Array<String>)

    keys pointing to sets to unify

Returns:

  • (Array<String>)

    members in the union



161
162
163
164
# File 'lib/redis/commands/sets.rb', line 161

def sunion(*keys)
  keys.flatten!(1)
  send_command([:sunion].concat(keys))
end

#sunionstore(destination, *keys) ⇒ Integer

Add multiple sets and store the resulting set in a key.

Parameters:

  • destination (String)

    destination key

  • keys (String, Array<String>)

    keys pointing to sets to unify

Returns:

  • (Integer)

    number of elements in the resulting set



171
172
173
174
# File 'lib/redis/commands/sets.rb', line 171

def sunionstore(destination, *keys)
  keys.flatten!(1)
  send_command([:sunionstore, destination].concat(keys))
end