Module: RedisCluster::Function::SortedSet

Included in:
RedisCluster::Function
Defined in:
lib/redis_cluster/function/sorted_set.rb

Overview

SortedSet implement redis sorted set commands. There will be some adjustment for cluster. see redis.io/commands#sorted_set. Most of the code are copied from github.com/redis/redis-rb/blob/master/lib/redis.rb.

SETTER = [:zadd, :zincrby, :zrem, :zremrangebyrank, :zremrangebyscore] GETTER = [:zcard, :zscore, :zrange, :zrevrange, :zrank, :zrevrange, :zrangebylex,

:zrevrangebylex, :zrangebyscore, :zrevrangebyscore, :zcount]

Instance Method Summary collapse

Instance Method Details

#zadd(key, *args) ⇒ Boolean, ...

Add one or more members to a sorted set, or update the score for members that already exist.

Examples:

Add a single ‘[score, member]` pair to a sorted set

redis.zadd("zset", 32.0, "member")

Add an array of ‘[score, member]` pairs to a sorted set

redis.zadd("zset", [[32.0, "a"], [64.0, "b"]])

Parameters:

  • key (String)
  • args ([Float, String], Array<[Float, String]>)
    • a single ‘[score, member]` pair

    • an array of ‘[score, member]` pairs

  • options (Hash)
    • ‘:xx => true`: Only update elements that already exist (never

    add elements)

    • ‘:nx => true`: Don’t update already existing elements (always

    add new elements)

    • ‘:ch => true`: Modify the return value from the number of new

    elements added, to the total number of elements changed (CH is an abbreviation of changed); changed elements are new elements added and elements already existing for which the score was updated

    • ‘:incr => true`: When this option is specified ZADD acts like

    ZINCRBY; only one score-element pair can be specified in this mode

Returns:

  • (Boolean, Fixnum, Float)
    • ‘Boolean` when a single pair is specified, holding whether or not it was

    added to the sorted set.

    • ‘Fixnum` when an array of pairs is specified, holding the number of

    pairs that were added to the sorted set.

    • ‘Float` when option :incr is specified, holding the score of the member

    after incrementing it.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/redis_cluster/function/sorted_set.rb', line 60

def zadd(key, *args)
  zadd_options = [:zadd, key]
  if args.last.is_a?(::Hash)
    options = args.pop
    incr = options[:incr]

    zadd_options << 'NX' if options[:nx]
    zadd_options << 'XX' if options[:xx]
    zadd_options << 'CH' if options[:ch]
    zadd_options << 'INCR' if incr
  end

  if args.size == 1 && args[0].is_a?(Array)
    # Variadic: return float if INCR, integer if !INCR
    call(key, zadd_options + args[0], transform: (incr ? Redis::Floatify : nil))
  elsif args.size == 2
    # Single pair: return float if INCR, boolean if !INCR
    call(key, zadd_options + args, transform: (incr ? Redis::Floatify : Redis::Boolify))
  else
    raise ArgumentError, 'wrong number of arguments'
  end
end

#zcard(key) ⇒ Fixnum

Get the number of members in a sorted set.

Examples:

redis.zcard("zset")
  # => 4

Parameters:

Returns:

  • (Fixnum)


25
26
27
# File 'lib/redis_cluster/function/sorted_set.rb', line 25

def zcard(key)
  call(key, [:zcard, key], read: true)
end

#zcount(key, min, max) ⇒ Fixnum

Count the members in a sorted set with scores within the given values.

Examples:

Count members with score ‘>= 5` and `< 100`

redis.zcount("zset", "5", "(100")
  # => 2

Count members with scores ‘> 5`

redis.zcount("zset", "(5", "+inf")
  # => 2

Parameters:

  • key (String)
  • min (String)
    • inclusive minimum score is specified verbatim

    • exclusive minimum score is specified by prefixing ‘(`

  • max (String)
    • inclusive maximum score is specified verbatim

    • exclusive maximum score is specified by prefixing ‘(`

Returns:

  • (Fixnum)

    number of members in within the specified range



371
372
373
# File 'lib/redis_cluster/function/sorted_set.rb', line 371

def zcount(key, min, max)
  call(key, [:zcount, key, min, max], read: true)
end

#zincrby(key, increment, member) ⇒ Float

Increment the score of a member in a sorted set.

Examples:

redis.zincrby("zset", 32.0, "a")
  # => 64.0

Parameters:

Returns:

  • (Float)

    score of the member after incrementing it



93
94
95
# File 'lib/redis_cluster/function/sorted_set.rb', line 93

def zincrby(key, increment, member)
  call(key, [:zincrby, key, increment, member], transform: Redis::Floatify)
end

#zrange(key, start, stop, options = {}) ⇒ Array<String>, Array<[String, Float]>

Return a range of members in a sorted set, by index.

Examples:

Retrieve all members from a sorted set

redis.zrange("zset", 0, -1)
  # => ["a", "b"]

Retrieve all members and their scores from a sorted set

redis.zrange("zset", 0, -1, :withscores => true)
  # => [["a", 32.0], ["b", 64.0]]

Parameters:

  • key (String)
  • start (Fixnum)

    start index

  • stop (Fixnum)

    stop index

  • options (Hash) (defaults to: {})
    • ‘:withscores => true`: include scores in output

Returns:

  • (Array<String>, Array<[String, Float]>)
    • when ‘:withscores` is not specified, an array of members

    • when ‘:withscores` is specified, an array with `[member, score]` pairs



145
146
147
148
149
150
151
152
153
154
# File 'lib/redis_cluster/function/sorted_set.rb', line 145

def zrange(key, start, stop, options = {})
  args = [:zrange, key, start, stop]

  if options[:withscores]
    args << 'WITHSCORES'
    block = Redis::FloatifyPairs
  end

  call(key, args, transform: block, read: true)
end

#zrangebylex(key, min, max, options = {}) ⇒ Array<String>, Array<[String, Float]>

Return a range of members with the same score in a sorted set, by lexicographical ordering

Examples:

Retrieve members matching a

redis.zrangebylex("zset", "[a", "[a\xff")
  # => ["aaren", "aarika", "abagael", "abby"]

Retrieve the first 2 members matching a

redis.zrangebylex("zset", "[a", "[a\xff", :limit => [0, 2])
  # => ["aaren", "aarika"]

Parameters:

  • key (String)
  • min (String)
    • inclusive minimum is specified by prefixing ‘(`

    • exclusive minimum is specified by prefixing ‘[`

  • max (String)
    • inclusive maximum is specified by prefixing ‘(`

    • exclusive maximum is specified by prefixing ‘[`

  • options (Hash) (defaults to: {})
    • ‘:limit => [offset, count]`: skip `offset` members, return a maximum of

    ‘count` members

Returns:



235
236
237
238
239
240
241
242
# File 'lib/redis_cluster/function/sorted_set.rb', line 235

def zrangebylex(key, min, max, options = {})
  args = [:zrangebylex, key, min, max]

  limit = options[:limit]
  args.concat(['LIMIT'] + limit) if limit

  call(key, args, read: true)
end

#zrangebyscore(key, min, max, options = {}) ⇒ Array<String>, Array<[String, Float]>

Return a range of members in a sorted set, by score.

Examples:

Retrieve members with score ‘>= 5` and `< 100`

redis.zrangebyscore("zset", "5", "(100")
  # => ["a", "b"]

Retrieve the first 2 members with score ‘>= 0`

redis.zrangebyscore("zset", "0", "+inf", :limit => [0, 2])
  # => ["a", "b"]

Retrieve members and their scores with scores ‘> 5`

redis.zrangebyscore("zset", "(5", "+inf", :withscores => true)
  # => [["a", 32.0], ["b", 64.0]]

Parameters:

  • key (String)
  • min (String)
    • inclusive minimum score is specified verbatim

    • exclusive minimum score is specified by prefixing ‘(`

  • max (String)
    • inclusive maximum score is specified verbatim

    • exclusive maximum score is specified by prefixing ‘(`

  • options (Hash) (defaults to: {})
    • ‘:withscores => true`: include scores in output

    • ‘:limit => [offset, count]`: skip `offset` members, return a maximum of

    ‘count` members

Returns:

  • (Array<String>, Array<[String, Float]>)
    • when ‘:withscores` is not specified, an array of members

    • when ‘:withscores` is specified, an array with `[member, score]` pairs



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/redis_cluster/function/sorted_set.rb', line 291

def zrangebyscore(key, min, max, options = {})
  args = [:zrangebyscore, key, min, max]

  if options[:withscores]
    args << 'WITHSCORES'
    block = Redis::FloatifyPairs
  end

  limit = options[:limit]
  args.concat(['LIMIT'] + limit) if limit

  call(key, args, transform: block, read: true)
end

#zrank(key, member) ⇒ Fixnum

Determine the index of a member in a sorted set.

Parameters:

Returns:

  • (Fixnum)


183
184
185
# File 'lib/redis_cluster/function/sorted_set.rb', line 183

def zrank(key, member)
  call(key, [:zrank, key, member], read: true)
end

#zrem(key, member) ⇒ Fixnum

Remove one or more members from a sorted set.

Examples:

Remove a single member from a sorted set

redis.zrem("zset", "a")

Remove an array of members from a sorted set

redis.zrem("zset", ["a", "b"])

Parameters:

Returns:

  • (Fixnum)

    number of members that were removed to the sorted set



110
111
112
# File 'lib/redis_cluster/function/sorted_set.rb', line 110

def zrem(key, member)
  call(key, [:zrem, key, member])
end

#zremrangebyrank(key, start, stop) ⇒ Fixnum

Remove all members in a sorted set within the given indexes.

Examples:

Remove first 5 members

redis.zremrangebyrank("zset", 0, 4)
  # => 5

Remove last 5 members

redis.zremrangebyrank("zset", -5, -1)
  # => 5

Parameters:

  • key (String)
  • start (Fixnum)

    start index

  • stop (Fixnum)

    stop index

Returns:

  • (Fixnum)

    number of members that were removed



210
211
212
# File 'lib/redis_cluster/function/sorted_set.rb', line 210

def zremrangebyrank(key, start, stop)
  call(key, [:zremrangebyrank, key, start, stop])
end

#zremrangebyscore(key, min, max) ⇒ Fixnum

Remove all members in a sorted set within the given scores.

Examples:

Remove members with score ‘>= 5` and `< 100`

redis.zremrangebyscore("zset", "5", "(100")
  # => 2

Remove members with scores ‘> 5`

redis.zremrangebyscore("zset", "(5", "+inf")
  # => 2

Parameters:

  • key (String)
  • min (String)
    • inclusive minimum score is specified verbatim

    • exclusive minimum score is specified by prefixing ‘(`

  • max (String)
    • inclusive maximum score is specified verbatim

    • exclusive maximum score is specified by prefixing ‘(`

Returns:

  • (Fixnum)

    number of members that were removed



350
351
352
# File 'lib/redis_cluster/function/sorted_set.rb', line 350

def zremrangebyscore(key, min, max)
  call(key, [:zremrangebyscore, key, min, max])
end

#zrevrange(key, start, stop, options = {}) ⇒ Object

Return a range of members in a sorted set, by index, with scores ordered from high to low.

Examples:

Retrieve all members from a sorted set

redis.zrevrange("zset", 0, -1)
  # => ["b", "a"]

Retrieve all members and their scores from a sorted set

redis.zrevrange("zset", 0, -1, :withscores => true)
  # => [["b", 64.0], ["a", 32.0]]

See Also:



167
168
169
170
171
172
173
174
175
176
# File 'lib/redis_cluster/function/sorted_set.rb', line 167

def zrevrange(key, start, stop, options = {})
  args = [:zrevrange, key, start, stop]

  if options[:withscores]
    args << 'WITHSCORES'
    block = Redis::FloatifyPairs
  end

  call(key, args, transform: block, read: true)
end

#zrevrangebylex(key, max, min, options = {}) ⇒ Object

Return a range of members with the same score in a sorted set, by reversed lexicographical ordering. Apart from the reversed ordering, #zrevrangebylex is similar to #zrangebylex.

Examples:

Retrieve members matching a

redis.zrevrangebylex("zset", "[a", "[a\xff")
  # => ["abbygail", "abby", "abagael", "aaren"]

Retrieve the last 2 members matching a

redis.zrevrangebylex("zset", "[a", "[a\xff", :limit => [0, 2])
  # => ["abbygail", "abby"]

See Also:



255
256
257
258
259
260
261
262
# File 'lib/redis_cluster/function/sorted_set.rb', line 255

def zrevrangebylex(key, max, min, options = {})
  args = [:zrevrangebylex, key, min, max]

  limit = options[:limit]
  args.concat(['LIMIT'] + limit) if limit

  call(key, args, read: true)
end

#zrevrangebyscore(key, max, min, options = {}) ⇒ Object

Return a range of members in a sorted set, by score, with scores ordered from high to low.

Examples:

Retrieve members with score ‘< 100` and `>= 5`

redis.zrevrangebyscore("zset", "(100", "5")
  # => ["b", "a"]

Retrieve the first 2 members with score ‘<= 0`

redis.zrevrangebyscore("zset", "0", "-inf", :limit => [0, 2])
  # => ["b", "a"]

Retrieve members and their scores with scores ‘> 5`

redis.zrevrangebyscore("zset", "+inf", "(5", :withscores => true)
  # => [["b", 64.0], ["a", 32.0]]

See Also:



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/redis_cluster/function/sorted_set.rb', line 319

def zrevrangebyscore(key, max, min, options = {})
  args = [:zrevrangebyscore, key, min, max]

  if options[:withscores]
    args << 'WITHSCORES'
    block = Redis::FloatifyPairs
  end

  limit = options[:limit]
  args.concat(['LIMIT'] + limit) if limit

  call(key, args, transform: block, read: true)
end

#zrevrank(key, member) ⇒ Fixnum

Determine the index of a member in a sorted set, with scores ordered from high to low.

Parameters:

Returns:

  • (Fixnum)


193
194
195
# File 'lib/redis_cluster/function/sorted_set.rb', line 193

def zrevrank(key, member)
  call(key, [:zrevrank, key, member], read: true)
end

#zscore(key, member) ⇒ Float

Get the score associated with the given member in a sorted set.

Examples:

Get the score for member “a”

redis.zscore("zset", "a")
  # => 32.0

Parameters:

Returns:

  • (Float)

    score of the member



123
124
125
# File 'lib/redis_cluster/function/sorted_set.rb', line 123

def zscore(key, member)
  call(key, [:zscore, key, member], transform: Redis::Floatify, read: true)
end