123456789_123456789_123456789_123456789_123456789_

Module: Redis::Commands::Sets

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/redis/commands/sets.rb

Instance Method Summary

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

[ GitHub ]

  
# 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.

[ GitHub ]

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

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

#saddex(key, *members, mnx: nil, mxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false) ⇒ Integer

Adds one or more members to a set and optionally sets their expiration. Without an expiration option, an existing member's TTL is cleared unless :keepttl is given.

Examples:

redis.saddex("set", "m1", "m2", ex: 60) # => 1
redis.saddex("set", ["m1", "m3"], mnx: true, ex: 60) # => 0

Parameters:

  • key (String)
  • members (Array<String>)
  • options (Hash) —
    • :mnx => true: Only apply if none of the members already exist. Mutually exclusive with :mxx.
    • :mxx => true: Only apply if all of the members already exist. Mutually exclusive with :mnx.
    • :ex => Integer: Set the specified expire time on the members, in seconds.
    • :px => Integer: Set the specified expire time on the members, in milliseconds.
    • :exat => Integer: Set the specified Unix time at which the members will expire, in seconds.
    • :pxat => Integer: Set the specified Unix time at which the members will expire, in milliseconds.
    • :keepttl => true: Retain the time to live already associated with the members.

Returns:

  • (Integer) —

    1 if the members were added/updated, 0 if the :mnx/:mxx condition was not met

Raises:

  • (ArgumentError) —

    when mutually exclusive options are combined

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 507

def saddex(key, *members, mnx: nil, mxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false)
  raise ArgumentError, "mnx and mxx are mutually exclusive" if mnx && mxx
  if [ex, px, exat, pxat, keepttl].count { |option| option } > 1
    raise ArgumentError, "ex, px, exat, pxat, and keepttl are mutually exclusive"
  end

  members.flatten!(1)

  args = [:saddex, key]
  args << "MNX" if mnx
  args << "MXX" if mxx
  args << "EX" << Integer(ex) if ex
  args << "PX" << Integer(px) if px
  args << "EXAT" << Integer(exat) if exat
  args << "PXAT" << Integer(pxat) if pxat
  args << "KEEPTTL" if keepttl
  args.concat(['MEMBERS', members.length, *members])

  send_command(args)
end

#scard(key) ⇒ Integer

Get the number of members in a set.

Parameters:

  • key (String)
[ GitHub ]

  
# 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

[ GitHub ]

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

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

#sdiffcard(*keys, limit: nil) ⇒ Integer

Get the number of members in the difference between the first set and all successive sets.

Examples:

redis.sadd("foo", ["s1", "s2", "s3"])
redis.sadd("bar", "s3")
redis.sdiffcard("foo", "bar")
  # => 2

Parameters:

  • keys (String, Array<String>) —

    keys pointing to sets to subtract

  • limit (Integer) —

    stop counting once the difference reaches limit members (0 means unlimited)

Returns:

  • (Integer) —

    number of members in the difference

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 151

def sdiffcard(*keys, limit: nil)
  keys.flatten!(1)
  args = [:sdiffcard, keys.size].concat(keys)
  args << "LIMIT" << Integer(limit) if limit
  send_command(args)
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

[ GitHub ]

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

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

#sexpire(key, ttl, *members, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>

Sets the time to live in seconds for one or more set members. An expired member is removed from the set; a TTL of 0 removes it immediately.

Examples:

redis.sadd("set", "m1")
redis.sexpire("set", 10, "m1", "m2") # => [1, -2]
redis.sexpire("set", 10, "m1", "m2", nx: true) # => [0, -2]

Parameters:

  • key (String)
  • ttl (Integer)
  • options (Hash) —
    • :nx => true: Set expiry only when the member has no expiry.
    • :xx => true: Set expiry only when the member has an existing expiry.
    • :gt => true: Set expiry only when the new expiry is greater than current one.
    • :lt => true: Set expiry only when the new expiry is less than current one.
  • members (Array<String>)

Returns:

  • (Array<Integer>) —

    per member: 1 set, 0 condition not met, 2 deleted (TTL 0), -2 no such member or key.

Raises:

  • (ArgumentError) —

    when more than one of :nx, :xx, :gt, :lt is given

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 300

def sexpire(key, ttl, *members, nx: nil, xx: nil, gt: nil, lt: nil)
  members.flatten!(1)
  args = [:sexpire, key, Integer(ttl)]
  args.concat(hash_field_expiration_condition(nx, xx, gt, lt))
  args.concat(['MEMBERS', members.length, *members])

  send_command(args)
end

#sexpireat(key, unix_time_seconds, *members, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>

Sets the expiration for one or more set members as an absolute Unix timestamp in seconds. A timestamp in the past removes the member immediately.

Examples:

redis.sadd("set", "m1")
redis.sexpireat("set", Time.now.to_i + 10, "m1", "m2") # => [1, -2]
redis.sexpireat("set", Time.now.to_i - 10, "m1") # => [2]

Parameters:

  • key (String)
  • unix_time_seconds (Integer) —

    absolute expiration timestamp in seconds since epoch

  • options (Hash) —
    • :nx => true: Set expiry only when the member has no expiry.
    • :xx => true: Set expiry only when the member has an existing expiry.
    • :gt => true: Set expiry only when the new expiry is greater than current one.
    • :lt => true: Set expiry only when the new expiry is less than current one.
  • members (Array<String>)

Returns:

  • (Array<Integer>) —

    per member: 1 set, 0 condition not met, 2 deleted (past timestamp), -2 no such member or key.

Raises:

  • (ArgumentError) —

    when more than one of :nx, :xx, :gt, :lt is given

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 358

def sexpireat(key, unix_time_seconds, *members, nx: nil, xx: nil, gt: nil, lt: nil)
  members.flatten!(1)
  args = [:sexpireat, key, Integer(unix_time_seconds)]
  args.concat(hash_field_expiration_condition(nx, xx, gt, lt))
  args.concat(['MEMBERS', members.length, *members])

  send_command(args)
end

#sexpiretime(key, *members) ⇒ Array<Integer>

Returns the expiration time of one or more set members as an absolute Unix timestamp in seconds.

Examples:

redis.sadd("set", "m1", "m2")
redis.sexpireat("set", Time.now.to_i + 100, "m1")
redis.sexpiretime("set", "m1", "m2", "m3") # => [<unix timestamp in seconds>, -1, -2]

Parameters:

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

Returns:

  • (Array<Integer>) —

    per member: the expiration Unix timestamp in seconds, -1 when the member has no expiry, -2 when the member or key does not exist.

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 441

def sexpiretime(key, *members)
  members.flatten!(1)
  send_command([:sexpiretime, key, 'MEMBERS', members.length, *members])
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

[ GitHub ]

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

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

#sintercard(*keys, limit: nil) ⇒ Integer

Get the number of members in the intersection of multiple sets.

Examples:

redis.sadd("foo", ["s1", "s2", "s3"])
redis.sadd("bar", ["s2", "s3", "s4"])
redis.sintercard("foo", "bar")
  # => 2

Stop counting once the intersection reaches 1 member

redis.sintercard("foo", "bar", limit: 1)
  # => 1

Parameters:

  • keys (String, Array<String>) —

    keys pointing to sets to intersect

  • limit (Integer) —

    stop counting once the intersection reaches limit members (0 means unlimited)

Returns:

  • (Integer) —

    number of members in the intersection

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 182

def sintercard(*keys, limit: nil)
  keys.flatten!(1)
  args = [:sintercard, keys.size].concat(keys)
  args << "LIMIT" << Integer(limit) if limit
  send_command(args)
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

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 194

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)
[ GitHub ]

  
# 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)
[ GitHub ]

  
# 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>)
[ GitHub ]

  
# 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

[ GitHub ]

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

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

#spersist(key, *members) ⇒ Array<Integer>

Removes the expiration from one or more set members, making them persistent.

Examples:

redis.sadd("set", "m1", "m2")
redis.sexpire("set", 100, "m1")
redis.spersist("set", "m1", "m2", "m3") # => [1, -1, -2]

Parameters:

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

Returns:

  • (Array<Integer>) —

    per member: 1 when the expiration was removed, -1 when the member has no expiry, -2 when the member or key does not exist.

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 477

def spersist(key, *members)
  members.flatten!(1)
  send_command([:spersist, key, 'MEMBERS', members.length, *members])
end

#spexpire(key, ttl, *members, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>

Sets the time to live in milliseconds for one or more set members. An expired member is removed from the set; a TTL of 0 removes it immediately.

Examples:

redis.sadd("set", "m1")
redis.spexpire("set", 500, "m1", "m2") # => [1, -2]
redis.spexpire("set", 500, "m1", "m2", nx: true) # => [0, -2]

Parameters:

  • key (String)
  • ttl (Integer)
  • options (Hash) —
    • :nx => true: Set expiry only when the member has no expiry.
    • :xx => true: Set expiry only when the member has an existing expiry.
    • :gt => true: Set expiry only when the new expiry is greater than current one.
    • :lt => true: Set expiry only when the new expiry is less than current one.
  • members (Array<String>)

Returns:

  • (Array<Integer>) —

    per member: 1 set, 0 condition not met, 2 deleted (TTL 0), -2 no such member or key.

Raises:

  • (ArgumentError) —

    when more than one of :nx, :xx, :gt, :lt is given

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 329

def spexpire(key, ttl, *members, nx: nil, xx: nil, gt: nil, lt: nil)
  members.flatten!(1)
  args = [:spexpire, key, Integer(ttl)]
  args.concat(hash_field_expiration_condition(nx, xx, gt, lt))
  args.concat(['MEMBERS', members.length, *members])

  send_command(args)
end

#spexpireat(key, unix_time_milliseconds, *members, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>

Sets the expiration for one or more set members as an absolute Unix timestamp in milliseconds. A timestamp in the past removes the member immediately.

Examples:

redis.sadd("set", "m1")
redis.spexpireat("set", (Time.now.to_f * 1000).to_i + 500, "m1", "m2") # => [1, -2]
redis.spexpireat("set", (Time.now.to_f * 1000).to_i - 500, "m1") # => [2]

Parameters:

  • key (String)
  • unix_time_milliseconds (Integer) —

    absolute expiration timestamp in milliseconds since epoch

  • options (Hash) —
    • :nx => true: Set expiry only when the member has no expiry.
    • :xx => true: Set expiry only when the member has an existing expiry.
    • :gt => true: Set expiry only when the new expiry is greater than current one.
    • :lt => true: Set expiry only when the new expiry is less than current one.
  • members (Array<String>)

Returns:

  • (Array<Integer>) —

    per member: 1 set, 0 condition not met, 2 deleted (past timestamp), -2 no such member or key.

Raises:

  • (ArgumentError) —

    when more than one of :nx, :xx, :gt, :lt is given

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 387

def spexpireat(key, unix_time_milliseconds, *members, nx: nil, xx: nil, gt: nil, lt: nil)
  members.flatten!(1)
  args = [:spexpireat, key, Integer(unix_time_milliseconds)]
  args.concat(hash_field_expiration_condition(nx, xx, gt, lt))
  args.concat(['MEMBERS', members.length, *members])

  send_command(args)
end

#spexpiretime(key, *members) ⇒ Array<Integer>

Returns the expiration time of one or more set members as an absolute Unix timestamp in milliseconds.

Examples:

redis.sadd("set", "m1", "m2")
redis.spexpireat("set", (Time.now.to_f * 1000).to_i + 100_000, "m1")
redis.spexpiretime("set", "m1", "m2", "m3") # => [<unix timestamp in milliseconds>, -1, -2]

Parameters:

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

Returns:

  • (Array<Integer>) —

    per member: the expiration Unix timestamp in milliseconds, -1 when the member has no expiry, -2 when the member or key does not exist.

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 459

def spexpiretime(key, *members)
  members.flatten!(1)
  send_command([:spexpiretime, key, 'MEMBERS', members.length, *members])
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)
[ GitHub ]

  
# 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

#spttl(key, *members) ⇒ Array<Integer>

Returns the time to live in milliseconds for one or more set members.

Examples:

redis.sadd("set", "m1", "m2")
redis.spexpire("set", 500, "m1") # => [1]
redis.spttl("set", "m1", "m2", "m3") # => [500, -1, -2]

Parameters:

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

Returns:

  • (Array<Integer>) —

    per member: the TTL in milliseconds, -1 when the member has no expiry, -2 when the member or key does not exist.

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 423

def spttl(key, *members)
  members.flatten!(1)
  send_command([:spttl, key, 'MEMBERS', members.length, *members])
end

#srandmember(key, count = nil) ⇒ String

Get one or more random members from a set.

Parameters:

  • key (String)
  • count (Integer) (defaults to: nil)
[ GitHub ]

  
# 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

[ GitHub ]

  
# 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.

[ GitHub ]

  
# 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 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

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 252

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 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

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 269

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

#sttl(key, *members) ⇒ Array<Integer>

Returns the time to live in seconds for one or more set members.

Examples:

redis.sadd("set", "m1", "m2")
redis.sexpire("set", 10, "m1") # => [1]
redis.sttl("set", "m1", "m2", "m3") # => [10, -1, -2]

Parameters:

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

Returns:

  • (Array<Integer>) —

    per member: the TTL in seconds, -1 when the member has no expiry, -2 when the member or key does not exist.

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 407

def sttl(key, *members)
  members.flatten!(1)
  send_command([:sttl, key, 'MEMBERS', members.length, *members])
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

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 203

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

#sunioncard(*keys, approx: false, limit: nil) ⇒ Integer

Get the number of distinct members in the union of multiple sets.

Examples:

redis.sadd("foo", ["s1", "s2", "s3"])
redis.sadd("bar", ["s3", "s4"])
redis.sunioncard("foo", "bar")
  # => 4

Parameters:

  • keys (String, Array<String>) —

    keys pointing to sets to unify

  • approx (Boolean) —

    return an approximate cardinality computed with HyperLogLog

  • limit (Integer) —

    stop counting once the union reaches limit members (0 means unlimited)

Returns:

  • (Integer) —

    number of distinct members in the union

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 231

def sunioncard(*keys, approx: false, limit: nil)
  keys.flatten!(1)
  args = [:sunioncard, keys.size].concat(keys)
  args << "APPROX" if approx
  args << "LIMIT" << Integer(limit) if limit
  send_command(args)
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

[ GitHub ]

  
# File 'lib/redis/commands/sets.rb', line 213

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