Module: Redis::Commands::SortedSets
- Included in:
- Redis::Commands
- Defined in:
- lib/redis/commands/sorted_sets.rb
Instance Method Summary collapse
-
#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.
-
#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.
-
#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) ⇒ Integer
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) ⇒ Object
Return a range of members in a sorted set, by index, with scores ordered from high to low.
-
#zrevrangebylex(key, max, min, limit: nil) ⇒ Object
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) ⇒ Object
Return a range of members in a sorted set, by score, with scores ordered from high to low.
-
#zrevrank(key, member) ⇒ Integer
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.
Instance Method Details
#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.
173 174 175 176 177 |
# File 'lib/redis/commands/sorted_sets.rb', line 173 def bzpopmax(*args) _bpop(:bzpopmax, args) do |reply| reply.is_a?(Array) ? [reply[0], reply[1], Floatify.call(reply[2])] : reply end end |
#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.
194 195 196 197 198 |
# File 'lib/redis/commands/sorted_sets.rb', line 194 def bzpopmin(*args) _bpop(:bzpopmin, args) do |reply| reply.is_a?(Array) ? [reply[0], reply[1], Floatify.call(reply[2])] : reply end end |
#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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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) # Variadic: return float if INCR, integer if !INCR send_command(command + args[0], &(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.
14 15 16 |
# 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.
600 601 602 |
# File 'lib/redis/commands/sorted_sets.rb', line 600 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
709 710 711 |
# File 'lib/redis/commands/sorted_sets.rb', line 709 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
725 726 727 |
# File 'lib/redis/commands/sorted_sets.rb', line 725 def zdiffstore(*args) _zsets_operation_store(:zdiffstore, *args) end |
#zincrby(key, increment, member) ⇒ Float
Increment the score of a member in a sorted set.
83 84 85 |
# File 'lib/redis/commands/sorted_sets.rb', line 83 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
623 624 625 |
# File 'lib/redis/commands/sorted_sets.rb', line 623 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.
642 643 644 |
# File 'lib/redis/commands/sorted_sets.rb', line 642 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.
431 432 433 |
# File 'lib/redis/commands/sorted_sets.rb', line 431 def zlexcount(key, min, max) send_command([:zlexcount, key, min, max]) end |
#zmscore(key, *members) ⇒ Array<Float>
Get the scores associated with the given members in a sorted set.
222 223 224 225 226 |
# File 'lib/redis/commands/sorted_sets.rb', line 222 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.
130 131 132 133 134 135 |
# File 'lib/redis/commands/sorted_sets.rb', line 130 def zpopmax(key, count = nil) send_command([:zpopmax, key, count].compact) 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.
151 152 153 154 155 156 |
# File 'lib/redis/commands/sorted_sets.rb', line 151 def zpopmin(key, count = nil) send_command([:zpopmin, key, count].compact) 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.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/redis/commands/sorted_sets.rb', line 250 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 << 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.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/redis/commands/sorted_sets.rb', line 289 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) 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
456 457 458 459 460 461 462 463 464 465 |
# File 'lib/redis/commands/sorted_sets.rb', line 456 def zrangebylex(key, min, max, limit: nil) args = [:zrangebylex, key, min, max] if limit args << "LIMIT" args.concat(limit) 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.
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/redis/commands/sorted_sets.rb', line 516 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) 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.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/redis/commands/sorted_sets.rb', line 331 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) end send_command(args) end |
#zrank(key, member) ⇒ Integer
Determine the index of a member in a sorted set.
382 383 384 |
# File 'lib/redis/commands/sorted_sets.rb', line 382 def zrank(key, member) send_command([:zrank, key, member]) end |
#zrem(key, member) ⇒ Boolean, Integer
Remove one or more members from a sorted set.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/redis/commands/sorted_sets.rb', line 104 def zrem(key, member) 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.
409 410 411 |
# File 'lib/redis/commands/sorted_sets.rb', line 409 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.
579 580 581 |
# File 'lib/redis/commands/sorted_sets.rb', line 579 def zremrangebyscore(key, min, max) send_command([:zremrangebyscore, key, min, max]) end |
#zrevrange(key, start, stop, withscores: false, with_scores: withscores) ⇒ Object
Return a range of members in a sorted set, by index, with scores ordered from high to low.
366 367 368 369 370 371 372 373 374 375 |
# File 'lib/redis/commands/sorted_sets.rb', line 366 def zrevrange(key, start, stop, withscores: false, with_scores: withscores) args = [:zrevrange, key, start, stop] if with_scores args << "WITHSCORES" block = FloatifyPairs end send_command(args, &block) end |
#zrevrangebylex(key, max, min, limit: nil) ⇒ 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.
478 479 480 481 482 483 484 485 486 487 |
# File 'lib/redis/commands/sorted_sets.rb', line 478 def zrevrangebylex(key, max, min, limit: nil) args = [:zrevrangebylex, key, max, min] if limit args << "LIMIT" args.concat(limit) end send_command(args) end |
#zrevrangebyscore(key, max, min, withscores: false, with_scores: withscores, limit: nil) ⇒ Object
Return a range of members in a sorted set, by score, with scores ordered from high to low.
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/redis/commands/sorted_sets.rb', line 546 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) end send_command(args, &block) end |
#zrevrank(key, member) ⇒ Integer
Determine the index of a member in a sorted set, with scores ordered from high to low.
392 393 394 |
# File 'lib/redis/commands/sorted_sets.rb', line 392 def zrevrank(key, member) send_command([:zrevrank, key, member]) end |
#zscan(key, cursor, **options) ⇒ String, Array<[String, Float]>
Scan a sorted set
742 743 744 745 746 |
# File 'lib/redis/commands/sorted_sets.rb', line 742 def zscan(key, cursor, **) _scan(:zscan, cursor, [key], **) do |reply| [reply[0], FloatifyPairs.call(reply[1])] end end |
#zscan_each(key, **options, &block) ⇒ Enumerator
Scan a sorted set
759 760 761 762 763 764 765 766 767 768 |
# File 'lib/redis/commands/sorted_sets.rb', line 759 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.
209 210 211 |
# File 'lib/redis/commands/sorted_sets.rb', line 209 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
666 667 668 |
# File 'lib/redis/commands/sorted_sets.rb', line 666 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.
684 685 686 |
# File 'lib/redis/commands/sorted_sets.rb', line 684 def zunionstore(*args) _zsets_operation_store(:zunionstore, *args) end |