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)
-
- (Object) key
readonly
Returns the value of attribute key.
-
- (Object) options
readonly
Returns the value of attribute options.
Instance Method Summary (collapse)
- - (Object) ==(x)
-
- (Object) [](index, length = nil)
(also: #slice)
Same functionality as Ruby arrays.
-
- (Object) []=(member, score)
How to add values using a sorted set.
-
- (Object) add(member, score)
Add a member and its corresponding value to Redis.
-
- (Object) at(index)
Return the value at the given index.
-
- (Object) decrement(member, by = 1)
(also: #decr, #decrby)
Convenience to calling increment() with a negative number.
-
- (Object) delete(value)
Delete the value from the set.
-
- (Object) delete_if(&block)
Delete element if it matches block.
-
- (Object) difference(*sets)
(also: #diff, #^, #-)
Return the difference vs another set.
-
- (Object) diffstore(name, *sets)
Calculate the diff and store it in Redis as name.
-
- (Boolean) empty?
Returns true if the set has no members.
-
- (Object) first
Return the first element in the list.
-
- (Object) increment(member, by = 1)
(also: #incr, #incrby)
Increment the rank of that member atomically and return the new value.
-
- (Object) intersection(*sets)
(also: #intersect, #inter, #&)
Return the intersection with another set.
-
- (Object) interstore(name, *sets)
Calculate the intersection and store it in Redis as name.
-
- (Object) last
Return the last element in the list.
-
- (Object) length
(also: #size)
The number of members in the set.
-
- (Boolean) member?(value)
Return a boolean indicating whether value is a member.
-
- (Object) members(options = {})
Return all members of the sorted set with their scores.
-
- (Object) range(start_index, end_index, options = {})
Return a range of values from start_index to end_index.
-
- (Object) range_size(min, max)
The number of members within a range of scores.
-
- (Object) rangebyscore(min, max, options = {})
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).
-
- (Object) rank(member)
Return the rank of the member in the sorted set, with scores ordered from low to high.
-
- (Object) remrangebyrank(min, max)
Remove all elements in the sorted set at key with rank between start and end.
-
- (Object) remrangebyscore(min, max)
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).
-
- (Object) revrange(start_index, end_index, options = {})
Return a range of values from start_index to end_index in reverse order.
-
- (Object) revrangebyscore(min, max, options = {})
Forwards compat (not yet implemented in Redis).
- - (Object) revrank(member)
-
- (Object) score(member)
Return the score of the specified element of the sorted set at key.
- - (Object) to_s
-
- (Object) union(*sets)
(also: #|, #+)
Return the union with another set.
-
- (Object) unionstore(name, *sets)
Calculate the union and store it in Redis as name.
Methods included from Helpers::Serialize
Methods included from Helpers::CoreCommands
#exists?, #expire, #expireat, #move, #persist, #rename, #renamenx, #sort, #ttl, #type
Methods inherited from BaseObject
Constructor Details
This class inherits a constructor from Redis::BaseObject
Instance Attribute Details
- (Object) key (readonly)
Returns the value of attribute key
15 16 17 |
# File 'lib/redis/sorted_set.rb', line 15 def key @key end |
- (Object) options (readonly)
Returns the value of attribute options
15 16 17 |
# File 'lib/redis/sorted_set.rb', line 15 def @options end |
Instance Method Details
- (Object) ==(x)
252 253 254 |
# File 'lib/redis/sorted_set.rb', line 252 def ==(x) members == x end |
- (Object) [](index, length = nil) 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 result = score(index) || 0 # handles a nil score end end |
- (Object) []=(member, score)
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 |
- (Object) add(member, score)
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 |
- (Object) at(index)
Return the value at the given index. Can also use familiar list syntax. Redis: ZRANGE
262 263 264 |
# File 'lib/redis/sorted_set.rb', line 262 def at(index) range(index, index).first end |
- (Object) decrement(member, by = 1) Also known as: decr, decrby
Convenience to calling increment() with a negative number.
169 170 171 |
# File 'lib/redis/sorted_set.rb', line 169 def decrement(member, by=1) redis.zincrby(key, -by, member).to_i end |
- (Object) delete(value)
Delete the value from the set. Redis: ZREM
144 145 146 |
# File 'lib/redis/sorted_set.rb', line 144 def delete(value) redis.zrem(key, to_redis(value)) end |
- (Object) delete_if(&block)
Delete element if it matches block
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/redis/sorted_set.rb', line 149 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 |
- (Object) difference(*sets) 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
234 235 236 |
# File 'lib/redis/sorted_set.rb', line 234 def difference(*sets) from_redis redis.zdiff(key, *keys_from_objects(sets)) end |
- (Object) diffstore(name, *sets)
Calculate the diff and store it in Redis as name. Returns the number of elements in the stored union. Redis: SDIFFSTORE
243 244 245 |
# File 'lib/redis/sorted_set.rb', line 243 def diffstore(name, *sets) redis.zdiffstore(name, key, *keys_from_objects(sets)) end |
- (Boolean) empty?
Returns true if the set has no members. Redis: SCARD == 0
248 249 250 |
# File 'lib/redis/sorted_set.rb', line 248 def empty? length == 0 end |
- (Object) first
Return the first element in the list. Redis: ZRANGE(0)
267 268 269 |
# File 'lib/redis/sorted_set.rb', line 267 def first at(0) end |
- (Object) increment(member, by = 1) 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
162 163 164 |
# File 'lib/redis/sorted_set.rb', line 162 def increment(member, by=1) redis.zincrby(key, by, member).to_i end |
- (Object) intersection(*sets) 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
186 187 188 |
# File 'lib/redis/sorted_set.rb', line 186 def intersection(*sets) from_redis redis.zinter(key, *keys_from_objects(sets)) end |
- (Object) interstore(name, *sets)
Calculate the intersection and store it in Redis as name. Returns the number of elements in the stored intersection. Redis: SUNIONSTORE
195 196 197 |
# File 'lib/redis/sorted_set.rb', line 195 def interstore(name, *sets) redis.zinterstore(name, keys_from_objects([self] + sets)) end |
- (Object) last
Return the last element in the list. Redis: ZRANGE(-1)
272 273 274 |
# File 'lib/redis/sorted_set.rb', line 272 def last at(-1) end |
- (Object) length Also known as: size
The number of members in the set. Aliased as size. Redis: ZCARD
277 278 279 |
# File 'lib/redis/sorted_set.rb', line 277 def length redis.zcard(key) end |
- (Boolean) member?(value)
Return a boolean indicating whether value is a member.
288 289 290 |
# File 'lib/redis/sorted_set.rb', line 288 def member?(value) !redis.zscore(key, to_redis(value)).nil? end |
- (Object) members(options = {})
Return all members of the sorted set with their scores. Extremely CPU-intensive. Better to use a range instead.
80 81 82 83 |
# File 'lib/redis/sorted_set.rb', line 80 def members(={}) v = from_redis range(0, -1, ) v.nil? ? [] : v end |
- (Object) range(start_index, end_index, options = {})
Return a range of values from start_index to end_index. Can also use the familiar list Ruby syntax. Redis: ZRANGE
87 88 89 90 91 92 93 |
# File 'lib/redis/sorted_set.rb', line 87 def range(start_index, end_index, ={}) if [:withscores] || [:with_scores] from_redis redis.zrange(key, start_index, end_index, :with_scores => true) else from_redis redis.zrange(key, start_index, end_index) end end |
- (Object) range_size(min, max)
The number of members within a range of scores. Redis: ZCOUNT
283 284 285 |
# File 'lib/redis/sorted_set.rb', line 283 def range_size(min, max) redis.zcount(key, min, max) end |
- (Object) rangebyscore(min, max, options = {})
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
109 110 111 112 113 114 115 116 |
# File 'lib/redis/sorted_set.rb', line 109 def rangebyscore(min, max, ={}) args = {} args[:limit] = [[:offset] || 0, [:limit] || [:count]] if [:offset] || [:limit] || [:count] args[:with_scores] = true if [:withscores] || [:with_scores] from_redis redis.zrangebyscore(key, min, max, args) end |
- (Object) rank(member)
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
62 63 64 65 66 67 68 |
# File 'lib/redis/sorted_set.rb', line 62 def rank(member) if n = redis.zrank(key, to_redis(member)) n.to_i else nil end end |
- (Object) remrangebyrank(min, max)
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
133 134 135 |
# File 'lib/redis/sorted_set.rb', line 133 def remrangebyrank(min, max) redis.zremrangebyrank(key, min, max) end |
- (Object) remrangebyscore(min, max)
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
139 140 141 |
# File 'lib/redis/sorted_set.rb', line 139 def remrangebyscore(min, max) redis.zremrangebyscore(key, min, max) end |
- (Object) revrange(start_index, end_index, options = {})
Return a range of values from start_index to end_index in reverse order. Redis: ZREVRANGE
96 97 98 99 100 101 102 |
# File 'lib/redis/sorted_set.rb', line 96 def revrange(start_index, end_index, ={}) if [:withscores] || [:with_scores] from_redis redis.zrevrange(key, start_index, end_index, :with_scores => true) else from_redis redis.zrevrange(key, start_index, end_index) end end |
- (Object) revrangebyscore(min, max, options = {})
Forwards compat (not yet implemented in Redis)
119 120 121 122 123 124 125 126 |
# File 'lib/redis/sorted_set.rb', line 119 def revrangebyscore(min, max, ={}) args = {} args[:limit] = [[:offset] || 0, [:limit] || [:count]] if [:offset] || [:limit] || [:count] args[:with_scores] = true if [:withscores] || [:with_scores] from_redis redis.zrevrangebyscore(key, min, max, args) end |
- (Object) revrank(member)
70 71 72 73 74 75 76 |
# File 'lib/redis/sorted_set.rb', line 70 def revrank(member) if n = redis.zrevrank(key, to_redis(member)) n.to_i else nil end end |
- (Object) score(member)
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 55 56 |
# File 'lib/redis/sorted_set.rb', line 52 def score(member) result = redis.zscore(key, to_redis(member)) result.to_f unless result.nil? end |
- (Object) to_s
256 257 258 |
# File 'lib/redis/sorted_set.rb', line 256 def to_s members.join(', ') end |
- (Object) union(*sets) 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
210 211 212 |
# File 'lib/redis/sorted_set.rb', line 210 def union(*sets) from_redis redis.zunion(key, *keys_from_objects(sets)) end |
- (Object) unionstore(name, *sets)
Calculate the union and store it in Redis as name. Returns the number of elements in the stored union. Redis: SUNIONSTORE
218 219 220 |
# File 'lib/redis/sorted_set.rb', line 218 def unionstore(name, *sets) redis.zunionstore(name, keys_from_objects([self] + sets)) end |