Module: Redis::Commands::SortedSets
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/redis/commands/sorted_sets.rb |
Instance Method Summary
-
#bzmpop(timeout, *keys, modifier: "MIN", count: nil) ⇒ Array<String, Array<String, Float>>
Removes and returns up to count members with scores in the sorted set stored at key.
-
#bzpopmax(*args) ⇒ Array<String, String, Float>?
Removes and returns up to count members with the highest scores in the sorted set stored at keys,.
-
#bzpopmin(*args) ⇒ Array<String, String, Float>?
Removes and returns up to count members with the lowest scores in the sorted set stored at keys,.
-
#zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) ⇒ Boolean, ...
Add one or more members to a sorted set, or update the score for members that already exist.
-
#zcard(key) ⇒ Integer
Get the number of members in a sorted set.
-
#zcount(key, min, max) ⇒ Integer
Count the members in a sorted set with scores within the given values.
-
#zdiff(*keys, with_scores: false) ⇒ Array<String>, Array<[String, Float]>
Return the difference between the first and all successive input sorted sets.
-
#zdiffstore(*args) ⇒ Integer
Compute the difference between the first and all successive input sorted sets and store the resulting sorted set in a new key.
-
#zincrby(key, increment, member) ⇒ Float
Increment the score of a member in a sorted set.
-
#zinter(*args) ⇒ Array<String>, Array<[String, Float]>
Return the intersection of multiple sorted sets.
-
#zinterstore(*args) ⇒ Integer
Intersect multiple sorted sets and store the resulting sorted set in a new key.
-
#zlexcount(key, min, max) ⇒ Integer
Count the members, with the same score in a sorted set, within the given lexicographical range.
-
#zmpop(*keys, modifier: "MIN", count: nil) ⇒ Array<String, Array<String, Float>>
Removes and returns up to count members with scores in the sorted set stored at key.
-
#zmscore(key, *members) ⇒ Array<Float>
Get the scores associated with the given members in a sorted set.
-
#zpopmax(key, count = nil) ⇒ Array<String, Float>+
Removes and returns up to count members with the highest scores in the sorted set stored at key.
-
#zpopmin(key, count = nil) ⇒ Array<String, Float>+
Removes and returns up to count members with the lowest scores in the sorted set stored at key.
-
#zrandmember(key, count = nil, withscores: false, with_scores: withscores) ⇒ nil, ...
Get one or more random members from a sorted set.
-
#zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil, withscores: false, with_scores: withscores) ⇒ Array<String>, Array<[String, Float]>
Return a range of members in a sorted set, by index, score or lexicographical ordering.
-
#zrangebylex(key, min, max, limit: nil) ⇒ Array<String>, Array<[String, Float]>
Return a range of members with the same score in a sorted set, by lexicographical ordering.
-
#zrangebyscore(key, min, max, withscores: false, with_scores: withscores, limit: nil) ⇒ Array<String>, Array<[String, Float]>
Return a range of members in a sorted set, by score.
-
#zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil) ⇒ Integer
Select a range of members in a sorted set, by index, score or lexicographical ordering and store the resulting sorted set in a new key.
-
#zrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set.
-
#zrem(key, member) ⇒ Boolean, Integer
Remove one or more members from a sorted set.
-
#zremrangebyrank(key, start, stop) ⇒ Integer
Remove all members in a sorted set within the given indexes.
-
#zremrangebyscore(key, min, max) ⇒ Integer
Remove all members in a sorted set within the given scores.
-
#zrevrange(key, start, stop, withscores: false, with_scores: withscores)
Return a range of members in a sorted set, by index, with scores ordered from high to low.
-
#zrevrangebylex(key, max, min, limit: nil)
Return a range of members with the same score in a sorted set, by reversed lexicographical ordering.
-
#zrevrangebyscore(key, max, min, withscores: false, with_scores: withscores, limit: nil)
Return a range of members in a sorted set, by score, with scores ordered from high to low.
-
#zrevrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set, with scores ordered from high to low.
-
#zscan(key, cursor, **options) ⇒ String, Array<[String, Float]>
Scan a sorted set.
-
#zscan_each(key, **options, &block) ⇒ Enumerator
Scan a sorted set.
-
#zscore(key, member) ⇒ Float
Get the score associated with the given member in a sorted set.
-
#zunion(*args) ⇒ Array<String>, Array<[String, Float]>
Return the union of multiple sorted sets.
-
#zunionstore(*args) ⇒ Integer
Add multiple sorted sets and store the resulting sorted set in a new key.
- #_zsets_operation(cmd, *keys, weights: nil, aggregate: nil, with_scores: false) private
- #_zsets_operation_store(cmd, destination, keys, weights: nil, aggregate: nil) private
Instance Method Details
#_zsets_operation(cmd, *keys, weights: nil, aggregate: nil, with_scores: false) (private)
[ GitHub ]# File 'lib/redis/commands/sorted_sets.rb', line 888
def _zsets_operation(cmd, *keys, weights: nil, aggregate: nil, with_scores: false) keys.flatten!(1) command = [cmd, keys.size].concat(keys) if weights command << "WEIGHTS" command.concat(weights) end command << "AGGREGATE" << aggregate if aggregate if with_scores command << "WITHSCORES" block = FloatifyPairs end send_command(command, &block) end
#_zsets_operation_store(cmd, destination, keys, weights: nil, aggregate: nil) (private)
[ GitHub ]# File 'lib/redis/commands/sorted_sets.rb', line 907
def _zsets_operation_store(cmd, destination, keys, weights: nil, aggregate: nil) keys.flatten!(1) command = [cmd, destination, keys.size].concat(keys) if weights command << "WEIGHTS" command.concat(weights) end command << "AGGREGATE" << aggregate if aggregate send_command(command) end
#bzmpop(timeout, *keys, modifier: "MIN", count: nil) ⇒ Array
<String
, Array
<String
, Float
>>
Removes and returns up to count members with scores in the sorted set stored at key.
# File 'lib/redis/commands/sorted_sets.rb', line 188
def bzmpop(timeout, *keys, modifier: "MIN", count: nil) raise ArgumentError, "Pick either MIN or MAX" unless modifier == "MIN" || modifier == "MAX" args = [:bzmpop, timeout, keys.size, *keys, modifier] args << "COUNT" << Integer(count) if count send_blocking_command(args, timeout) do |response| response&.map do |entry| case entry when String then entry when Array then entry.map { |pair| FloatifyPairs.call(pair) }.flatten(1) end end end end
#bzpopmax(*args) ⇒ Array
<String
, String
, Float
>?
Removes and returns up to count members with the highest scores in the sorted set stored at keys, or block until one is available.
#bzpopmin(*args) ⇒ Array
<String
, String
, Float
>?
Removes and returns up to count members with the lowest scores in the sorted set stored at keys, or block until one is available.
#zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) ⇒ Boolean
, ...
Add one or more members to a sorted set, or update the score for members that already exist.
# File 'lib/redis/commands/sorted_sets.rb', line 53
def zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) command = [:zadd, key] command << "NX" if nx command << "XX" if xx command << "LT" if lt command << "GT" if gt command << "CH" if ch command << "INCR" if incr if args.size == 1 && args[0].is_a?(Array) members_to_add = args[0] return 0 if members_to_add.empty? # Variadic: return float if INCR, integer if !INCR send_command(command + members_to_add, &(incr ? Floatify : nil)) elsif args.size == 2 # Single pair: return float if INCR, boolean if !INCR send_command(command + args, &(incr ? Floatify : Boolify)) else raise ArgumentError, "wrong number of arguments" end end
#zcard(key) ⇒ Integer
Get the number of members in a sorted set.
# File 'lib/redis/commands/sorted_sets.rb', line 14
def zcard(key) send_command([:zcard, key]) end
#zcount(key, min, max) ⇒ Integer
Count the members in a sorted set with scores within the given values.
# File 'lib/redis/commands/sorted_sets.rb', line 712
def zcount(key, min, max) send_command([:zcount, key, min, max]) end
#zdiff(*keys, with_scores: false) ⇒ Array
<String
>, Array
<[String
, Float
]>
Return the difference between the first and all successive input sorted sets
# File 'lib/redis/commands/sorted_sets.rb', line 821
def zdiff(*keys, with_scores: false) _zsets_operation(:zdiff, *keys, with_scores: with_scores) end
#zdiffstore(*args) ⇒ Integer
Compute the difference between the first and all successive input sorted sets and store the resulting sorted set in a new key
# File 'lib/redis/commands/sorted_sets.rb', line 837
def zdiffstore(*args) _zsets_operation_store(:zdiffstore, *args) end
#zincrby(key, increment, member) ⇒ Float
Increment the score of a member in a sorted set.
# File 'lib/redis/commands/sorted_sets.rb', line 86
def zincrby(key, increment, member) send_command([:zincrby, key, increment, member], &Floatify) end
#zinter(*args) ⇒ Array
<String
>, Array
<[String
, Float
]>
Return the intersection of multiple sorted sets
# File 'lib/redis/commands/sorted_sets.rb', line 735
def zinter(*args) _zsets_operation(:zinter, *args) end
#zinterstore(*args) ⇒ Integer
Intersect multiple sorted sets and store the resulting sorted set in a new key.
# File 'lib/redis/commands/sorted_sets.rb', line 754
def zinterstore(*args) _zsets_operation_store(:zinterstore, *args) end
#zlexcount(key, min, max) ⇒ Integer
Count the members, with the same score in a sorted set, within the given lexicographical range.
# File 'lib/redis/commands/sorted_sets.rb', line 543
def zlexcount(key, min, max) send_command([:zlexcount, key, min, max]) end
#zmpop(*keys, modifier: "MIN", count: nil) ⇒ Array
<String
, Array
<String
, Float
>>
Removes and returns up to count members with scores in the sorted set stored at key.
# File 'lib/redis/commands/sorted_sets.rb', line 220
def zmpop(*keys, modifier: "MIN", count: nil) raise ArgumentError, "Pick either MIN or MAX" unless modifier == "MIN" || modifier == "MAX" args = [:zmpop, keys.size, *keys, modifier] args << "COUNT" << Integer(count) if count send_command(args) do |response| response&.map do |entry| case entry when String then entry when Array then entry.map { |pair| FloatifyPairs.call(pair) }.flatten(1) end end end end
#zmscore(key, *members) ⇒ Array
<Float
>
Get the scores associated with the given members in a sorted set.
# File 'lib/redis/commands/sorted_sets.rb', line 300
def zmscore(key, *members) send_command([:zmscore, key, *members]) do |reply| reply.map(&Floatify) end end
#zpopmax(key, count = nil) ⇒ Array
<String
, Float
>+
Removes and returns up to count members with the highest scores in the sorted set stored at key.
# File 'lib/redis/commands/sorted_sets.rb', line 138
def zpopmax(key, count = nil) command = [:zpopmax, key] command << Integer(count) if count send_command(command) do |members| members = FloatifyPairs.call(members) count.to_i > 1 ? members : members.first end end
#zpopmin(key, count = nil) ⇒ Array
<String
, Float
>+
Removes and returns up to count members with the lowest scores in the sorted set stored at key.
# File 'lib/redis/commands/sorted_sets.rb', line 161
def zpopmin(key, count = nil) command = [:zpopmin, key] command << Integer(count) if count send_command(command) do |members| members = FloatifyPairs.call(members) count.to_i > 1 ? members : members.first end end
#zrandmember(key, count = nil, withscores: false, with_scores: withscores) ⇒ nil
, ...
Get one or more random members from a sorted set.
# File 'lib/redis/commands/sorted_sets.rb', line 328
def zrandmember(key, count = nil, withscores: false, with_scores: withscores) if with_scores && count.nil? raise ArgumentError, "count argument must be specified" end args = [:zrandmember, key] args << Integer(count) if count if with_scores args << "WITHSCORES" block = FloatifyPairs end send_command(args, &block) end
#zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil, withscores: false, with_scores: withscores) ⇒ Array
<String
>, Array
<[String
, Float
]>
Return a range of members in a sorted set, by index, score or lexicographical ordering.
# File 'lib/redis/commands/sorted_sets.rb', line 367
def zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil, withscores: false, with_scores: withscores) if by_score && by_lex raise ArgumentError, "only one of :by_score or :by_lex can be specified" end args = [:zrange, key, start, stop] if by_score args << "BYSCORE" elsif by_lex args << "BYLEX" end args << "REV" if rev if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end if with_scores args << "WITHSCORES" block = FloatifyPairs end send_command(args, &block) end
#zrangebylex(key, min, max, limit: nil) ⇒ Array
<String
>, Array
<[String
, Float
]>
Return a range of members with the same score in a sorted set, by lexicographical ordering
# File 'lib/redis/commands/sorted_sets.rb', line 568
def zrangebylex(key, min, max, limit: nil) args = [:zrangebylex, key, min, max] if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(args) end
#zrangebyscore(key, min, max, withscores: false, with_scores: withscores, limit: nil) ⇒ Array
<String
>, Array
<[String
, Float
]>
Return a range of members in a sorted set, by score.
# File 'lib/redis/commands/sorted_sets.rb', line 628
def zrangebyscore(key, min, max, withscores: false, with_scores: withscores, limit: nil) args = [:zrangebyscore, key, min, max] if with_scores args << "WITHSCORES" block = FloatifyPairs end if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(args, &block) end
#zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil) ⇒ Integer
Select a range of members in a sorted set, by index, score or lexicographical ordering and store the resulting sorted set in a new key.
# File 'lib/redis/commands/sorted_sets.rb', line 409
def zrangestore(dest_key, src_key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil) if by_score && by_lex raise ArgumentError, "only one of :by_score or :by_lex can be specified" end args = [:zrangestore, dest_key, src_key, start, stop] if by_score args << "BYSCORE" elsif by_lex args << "BYLEX" end args << "REV" if rev if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(args) end
#zrank(key, member, withscore: false, with_score: withscore) ⇒ Integer
, [Integer
, Float
]
Determine the index of a member in a sorted set.
# File 'lib/redis/commands/sorted_sets.rb', line 470
def zrank(key, member, withscore: false, with_score: withscore) args = [:zrank, key, member] if with_score args << "WITHSCORE" block = FloatifyPair end send_command(args, &block) end
#zrem(key, member) ⇒ Boolean
, Integer
Remove one or more members from a sorted set.
# File 'lib/redis/commands/sorted_sets.rb', line 107
def zrem(key, member) if member.is_a?(Array) members_to_remove = member return 0 if members_to_remove.empty? end send_command([:zrem, key, member]) do |reply| if member.is_a? Array # Variadic: return integer reply else # Single argument: return boolean Boolify.call(reply) end end end
#zremrangebyrank(key, start, stop) ⇒ Integer
Remove all members in a sorted set within the given indexes.
# File 'lib/redis/commands/sorted_sets.rb', line 521
def zremrangebyrank(key, start, stop) send_command([:zremrangebyrank, key, start, stop]) end
#zremrangebyscore(key, min, max) ⇒ Integer
Remove all members in a sorted set within the given scores.
# File 'lib/redis/commands/sorted_sets.rb', line 691
def zremrangebyscore(key, min, max) send_command([:zremrangebyscore, key, min, max]) end
#zrevrange(key, start, stop, withscores: false, with_scores: withscores)
Return a range of members in a sorted set, by index, with scores ordered from high to low.
# File 'lib/redis/commands/sorted_sets.rb', line 444
def zrevrange(key, start, stop, withscores: false, with_scores: withscores) args = [:zrevrange, key, Integer(start), Integer(stop)] if with_scores args << "WITHSCORES" block = FloatifyPairs end send_command(args, &block) end
#zrevrangebylex(key, max, min, limit: nil)
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.
# File 'lib/redis/commands/sorted_sets.rb', line 590
def zrevrangebylex(key, max, min, limit: nil) args = [:zrevrangebylex, key, max, min] if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(args) end
#zrevrangebyscore(key, max, min, withscores: false, with_scores: withscores, limit: nil)
Return a range of members in a sorted set, by score, with scores ordered from high to low.
# File 'lib/redis/commands/sorted_sets.rb', line 658
def zrevrangebyscore(key, max, min, withscores: false, with_scores: withscores, limit: nil) args = [:zrevrangebyscore, key, max, min] if with_scores args << "WITHSCORES" block = FloatifyPairs end if limit args << "LIMIT" args.concat(limit.map { |l| Integer(l) }) end send_command(args, &block) end
#zrevrank(key, member, withscore: false, with_score: withscore) ⇒ Integer
, [Integer
, Float
]
Determine the index of a member in a sorted set, with scores ordered from high to low.
# File 'lib/redis/commands/sorted_sets.rb', line 497
def zrevrank(key, member, withscore: false, with_score: withscore) args = [:zrevrank, key, member] if with_score args << "WITHSCORE" block = FloatifyPair end send_command(args, &block) end
#zscan(key, cursor, **options) ⇒ String
, Array
<[String
, Float
]>
[ GitHub ]
# File 'lib/redis/commands/sorted_sets.rb', line 856
def zscan(key, cursor, ** ) _scan(:zscan, cursor, [key], ** ) do |reply| [reply[0], FloatifyPairs.call(reply[1])] end end
#zscan_each(key, **options, &block) ⇒ Enumerator
[ GitHub ]
# File 'lib/redis/commands/sorted_sets.rb', line 875
def zscan_each(key, **, &block) return to_enum(:zscan_each, key, ** ) unless block_given? cursor = 0 loop do cursor, values = zscan(key, cursor, ** ) values.each(&block) break if cursor == "0" end end
#zscore(key, member) ⇒ Float
Get the score associated with the given member in a sorted set.
# File 'lib/redis/commands/sorted_sets.rb', line 287
def zscore(key, member) send_command([:zscore, key, member], &Floatify) end
#zunion(*args) ⇒ Array
<String
>, Array
<[String
, Float
]>
Return the union of multiple sorted sets
# File 'lib/redis/commands/sorted_sets.rb', line 778
def zunion(*args) _zsets_operation(:zunion, *args) end
#zunionstore(*args) ⇒ Integer
Add multiple sorted sets and store the resulting sorted set in a new key.
# File 'lib/redis/commands/sorted_sets.rb', line 796
def zunionstore(*args) _zsets_operation_store(:zunionstore, *args) end