Class: Redis::SortedSet
- Inherits:
-
BaseObject
- Object
- BaseObject
- Redis::SortedSet
- Includes:
- Helpers::CoreCommands, Helpers::Serialize
- Defined in:
- lib/redis/sorted_set.rb
Overview
Class representing a sorted set.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #==(x) ⇒ Object
-
#[](index, length = nil) ⇒ Object
(also: #slice)
Same functionality as Ruby arrays.
-
#[]=(member, score) ⇒ Object
How to add values using a sorted set.
-
#add(member, score) ⇒ Object
Add a member and its corresponding value to Redis.
-
#at(index) ⇒ Object
Return the value at the given index.
-
#decrement(member, by = 1) ⇒ Object
(also: #decr, #decrby)
Convenience to calling increment() with a negative number.
-
#delete(value) ⇒ Object
Delete the value from the set.
-
#delete_if(&block) ⇒ Object
Delete element if it matches block.
-
#difference(*sets) ⇒ Object
(also: #diff, #^, #-)
Return the difference vs another set.
-
#diffstore(name, *sets) ⇒ Object
Calculate the diff and store it in Redis as
name
. -
#empty? ⇒ Boolean
Returns true if the set has no members.
-
#first ⇒ Object
Return the first element in the list.
-
#increment(member, by = 1) ⇒ Object
(also: #incr, #incrby)
Increment the rank of that member atomically and return the new value.
-
#intersection(*sets) ⇒ Object
(also: #intersect, #inter, #&)
Return the intersection with another set.
-
#interstore(name, *sets) ⇒ Object
Calculate the intersection and store it in Redis as
name
. -
#last ⇒ Object
Return the last element in the list.
-
#length ⇒ Object
(also: #size)
The number of members in the set.
-
#member?(value) ⇒ Boolean
Return a boolean indicating whether
value
is a member. -
#members(options = {}) ⇒ Object
Return all members of the sorted set with their scores.
-
#range(start_index, end_index, options = {}) ⇒ Object
Return a range of values from
start_index
toend_index
. -
#range_size(min, max) ⇒ Object
The number of members within a range of scores.
-
#rangebyscore(min, max, options = {}) ⇒ Object
Return the all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max).
-
#rank(member) ⇒ Object
Return the rank of the member in the sorted set, with scores ordered from low to high.
-
#remrangebyrank(min, max) ⇒ Object
Remove all elements in the sorted set at key with rank between start and end.
-
#remrangebyscore(min, max) ⇒ Object
Remove all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max).
-
#revrange(start_index, end_index, options = {}) ⇒ Object
Return a range of values from
start_index
toend_index
in reverse order. -
#revrangebyscore(min, max, options = {}) ⇒ Object
Forwards compat (not yet implemented in Redis).
- #revrank(member) ⇒ Object
-
#score(member) ⇒ Object
Return the score of the specified element of the sorted set at key.
- #to_s ⇒ Object
-
#union(*sets) ⇒ Object
(also: #|, #+)
Return the union with another set.
-
#unionstore(name, *sets) ⇒ Object
Calculate the union and store it in Redis as
name
.
Methods included from Helpers::Serialize
Methods included from Helpers::CoreCommands
#exists?, #expire, #expireat, #move, #rename, #renamenx, #sort, #ttl, #type
Methods inherited from BaseObject
Constructor Details
This class inherits a constructor from Redis::BaseObject
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
15 16 17 |
# File 'lib/redis/sorted_set.rb', line 15 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/redis/sorted_set.rb', line 15 def @options end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
15 16 17 |
# File 'lib/redis/sorted_set.rb', line 15 def redis @redis end |
Instance Method Details
#==(x) ⇒ Object
254 255 256 |
# File 'lib/redis/sorted_set.rb', line 254 def ==(x) members == x end |
#[](index, length = nil) ⇒ Object Also known as: slice
Same functionality as Ruby arrays. If a single number is given, return just the element at that index using Redis: ZRANGE. Otherwise, return a range of values using Redis: ZRANGE.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/redis/sorted_set.rb', line 34 def [](index, length=nil) if index.is_a? Range range(index.first, index.last) elsif length case length <=> 0 when 1 then range(index, index + length - 1) when 0 then [] when -1 then nil # Ruby does this (a bit weird) end else score(index) end end |
#[]=(member, score) ⇒ Object
How to add values using a sorted set. The key is the member, eg, “Peter”, and the value is the score, eg, 163. So:
num_posts['Peter'] = 163
20 21 22 |
# File 'lib/redis/sorted_set.rb', line 20 def []=(member, score) add(member, score) end |
#add(member, score) ⇒ Object
Add a member and its corresponding value to Redis. Note that the arguments to this are flipped; the member comes first rather than the score, since the member is the unique item (not the score).
27 28 29 |
# File 'lib/redis/sorted_set.rb', line 27 def add(member, score) redis.zadd(key, score, to_redis(member)) end |
#at(index) ⇒ Object
Return the value at the given index. Can also use familiar list syntax. Redis: ZRANGE
264 265 266 |
# File 'lib/redis/sorted_set.rb', line 264 def at(index) range(index, index).first end |
#decrement(member, by = 1) ⇒ Object Also known as: decr, decrby
Convenience to calling increment() with a negative number.
171 172 173 |
# File 'lib/redis/sorted_set.rb', line 171 def decrement(member, by=1) redis.zincrby(key, -by, member).to_i end |
#delete(value) ⇒ Object
Delete the value from the set. Redis: ZREM
146 147 148 |
# File 'lib/redis/sorted_set.rb', line 146 def delete(value) redis.zrem(key, to_redis(value)) end |
#delete_if(&block) ⇒ Object
Delete element if it matches block
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/redis/sorted_set.rb', line 151 def delete_if(&block) raise ArgumentError, "Missing block to SortedSet#delete_if" unless block_given? res = false redis.zrange(key, 0, -1).each do |m| if block.call(from_redis(m)) res = redis.zrem(key, m) end end res end |
#difference(*sets) ⇒ Object Also known as: diff, ^, -
Return the difference vs another set. Can pass it either another set object or set name. Also available as ^ or - which is a bit cleaner:
members_difference = set1 ^ set2
members_difference = set1 - set2
If you want to specify multiple sets, you must use difference
:
members_difference = set1.difference(set2, set3, set4)
members_difference = set1.diff(set2, set3, set4)
Redis: SDIFF
236 237 238 |
# File 'lib/redis/sorted_set.rb', line 236 def difference(*sets) from_redis redis.zdiff(key, *keys_from_objects(sets)) end |
#diffstore(name, *sets) ⇒ Object
Calculate the diff and store it in Redis as name
. Returns the number of elements in the stored union. Redis: SDIFFSTORE
245 246 247 |
# File 'lib/redis/sorted_set.rb', line 245 def diffstore(name, *sets) redis.zdiffstore(name, key, *keys_from_objects(sets)) end |
#empty? ⇒ Boolean
Returns true if the set has no members. Redis: SCARD == 0
250 251 252 |
# File 'lib/redis/sorted_set.rb', line 250 def empty? length == 0 end |
#first ⇒ Object
Return the first element in the list. Redis: ZRANGE(0)
269 270 271 |
# File 'lib/redis/sorted_set.rb', line 269 def first at(0) end |
#increment(member, by = 1) ⇒ Object Also known as: incr, incrby
Increment the rank of that member atomically and return the new value. This method is aliased as incr() for brevity. Redis: ZINCRBY
164 165 166 |
# File 'lib/redis/sorted_set.rb', line 164 def increment(member, by=1) redis.zincrby(key, by, member).to_i end |
#intersection(*sets) ⇒ Object Also known as: intersect, inter, &
Return the intersection with another set. Can pass it either another set object or set name. Also available as & which is a bit cleaner:
members_in_both = set1 & set2
If you want to specify multiple sets, you must use intersection
:
members_in_all = set1.intersection(set2, set3, set4)
members_in_all = set1.inter(set2, set3, set4) # alias
Redis: SINTER
188 189 190 |
# File 'lib/redis/sorted_set.rb', line 188 def intersection(*sets) from_redis redis.zinter(key, *keys_from_objects(sets)) end |
#interstore(name, *sets) ⇒ Object
Calculate the intersection and store it in Redis as name
. Returns the number of elements in the stored intersection. Redis: SUNIONSTORE
197 198 199 |
# File 'lib/redis/sorted_set.rb', line 197 def interstore(name, *sets) redis.zinterstore(name, key, *keys_from_objects(sets)) end |
#last ⇒ Object
Return the last element in the list. Redis: ZRANGE(-1)
274 275 276 |
# File 'lib/redis/sorted_set.rb', line 274 def last at(-1) end |
#length ⇒ Object Also known as: size
The number of members in the set. Aliased as size. Redis: ZCARD
279 280 281 |
# File 'lib/redis/sorted_set.rb', line 279 def length redis.zcard(key) end |
#member?(value) ⇒ Boolean
Return a boolean indicating whether value
is a member.
290 291 292 |
# File 'lib/redis/sorted_set.rb', line 290 def member?(value) !redis.zscore(key, to_redis(value)).nil? end |
#members(options = {}) ⇒ Object
Return all members of the sorted set with their scores. Extremely CPU-intensive. Better to use a range instead.
70 71 72 73 |
# File 'lib/redis/sorted_set.rb', line 70 def members(={}) v = from_redis range(0, -1, ) v.nil? ? [] : v end |
#range(start_index, end_index, options = {}) ⇒ Object
Return a range of values from start_index
to end_index
. Can also use the familiar list Ruby syntax. Redis: ZRANGE
77 78 79 80 81 82 83 84 |
# File 'lib/redis/sorted_set.rb', line 77 def range(start_index, end_index, ={}) if [:withscores] || [:with_scores] val = from_redis redis.zrange(key, start_index, end_index, :with_scores => true) group_set_with_scores(val) else from_redis redis.zrange(key, start_index, end_index) end end |
#range_size(min, max) ⇒ Object
The number of members within a range of scores. Redis: ZCOUNT
285 286 287 |
# File 'lib/redis/sorted_set.rb', line 285 def range_size(min, max) redis.zcount(key, min, max) end |
#rangebyscore(min, max, options = {}) ⇒ Object
Return the all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). Options:
:count, :offset - passed to LIMIT
:withscores - if true, scores are returned as well
Redis: ZRANGEBYSCORE
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/redis/sorted_set.rb', line 101 def rangebyscore(min, max, ={}) args = {} args[:limit] = [[:offset] || 0, [:limit] || [:count]] if [:offset] || [:limit] || [:count] args[:with_scores] = true if [:withscores] || [:with_scores] if args[:with_scores] val = from_redis redis.zrangebyscore(key, min, max, args) group_set_with_scores(val) else from_redis redis.zrangebyscore(key, min, max, args) end end |
#rank(member) ⇒ Object
Return the rank of the member in the sorted set, with scores ordered from low to high. revrank
returns the rank with scores ordered from high to low. When the given member does not exist in the sorted set, nil is returned. The returned rank (or index) of the member is 0-based for both commands
60 61 62 |
# File 'lib/redis/sorted_set.rb', line 60 def rank(member) redis.zrank(key, to_redis(member)).to_i end |
#remrangebyrank(min, max) ⇒ Object
Remove all elements in the sorted set at key with rank between start and end. Start and end are 0-based with rank 0 being the element with the lowest score. Both start and end can be negative numbers, where they indicate offsets starting at the element with the highest rank. For example: -1 is the element with the highest score, -2 the element with the second highest score and so forth. Redis: ZREMRANGEBYRANK
135 136 137 |
# File 'lib/redis/sorted_set.rb', line 135 def remrangebyrank(min, max) redis.zremrangebyrank(key, min, max) end |
#remrangebyscore(min, max) ⇒ Object
Remove all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). Redis: ZREMRANGEBYSCORE
141 142 143 |
# File 'lib/redis/sorted_set.rb', line 141 def remrangebyscore(min, max) redis.zremrangebyscore(key, min, max) end |
#revrange(start_index, end_index, options = {}) ⇒ Object
Return a range of values from start_index
to end_index
in reverse order. Redis: ZREVRANGE
87 88 89 90 91 92 93 94 |
# File 'lib/redis/sorted_set.rb', line 87 def revrange(start_index, end_index, ={}) if [:withscores] || [:with_scores] val = from_redis redis.zrevrange(key, start_index, end_index, :with_scores => true) group_set_with_scores(val) else from_redis redis.zrevrange(key, start_index, end_index) end end |
#revrangebyscore(min, max, options = {}) ⇒ Object
Forwards compat (not yet implemented in Redis)
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/redis/sorted_set.rb', line 116 def revrangebyscore(min, max, ={}) args = {} args[:limit] = [[:offset] || 0, [:limit] || [:count]] if [:offset] || [:limit] || [:count] args[:with_scores] = true if [:withscores] || [:with_scores] if args[:with_scores] val = from_redis redis.zrevrangebyscore(key, min, max, args) group_set_with_scores(val) else from_redis redis.zrevrangebyscore(key, min, max, args) end end |
#revrank(member) ⇒ Object
64 65 66 |
# File 'lib/redis/sorted_set.rb', line 64 def revrank(member) redis.zrevrank(key, to_redis(member)).to_i end |
#score(member) ⇒ Object
Return the score of the specified element of the sorted set at key. If the specified element does not exist in the sorted set, or the key does not exist at all, nil is returned. Redis: ZSCORE.
52 53 54 |
# File 'lib/redis/sorted_set.rb', line 52 def score(member) redis.zscore(key, to_redis(member)).to_f end |
#to_s ⇒ Object
258 259 260 |
# File 'lib/redis/sorted_set.rb', line 258 def to_s members.join(', ') end |
#union(*sets) ⇒ Object Also known as: |, +
Return the union with another set. Can pass it either another set object or set name. Also available as | and + which are a bit cleaner:
members_in_either = set1 | set2
members_in_either = set1 + set2
If you want to specify multiple sets, you must use union
:
members_in_all = set1.union(set2, set3, set4)
Redis: SUNION
212 213 214 |
# File 'lib/redis/sorted_set.rb', line 212 def union(*sets) from_redis redis.zunion(key, *keys_from_objects(sets)) end |
#unionstore(name, *sets) ⇒ Object
Calculate the union and store it in Redis as name
. Returns the number of elements in the stored union. Redis: SUNIONSTORE
220 221 222 |
# File 'lib/redis/sorted_set.rb', line 220 def unionstore(name, *sets) redis.zunionstore(name, key, *keys_from_objects(sets)) end |