Class: Redis::SortedSet
- Inherits:
-
BaseObject
- Object
- BaseObject
- Redis::SortedSet
- Includes:
- Helpers::CoreCommands
- 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.
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.
-
#merge(values) ⇒ Object
(also: #add_all)
Add a list of members and their corresponding value (or a hash mapping values to scores) to Redis.
-
#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(max, min, options = {}) ⇒ Object
Returns all the elements in the sorted set at key with a score between max and min (including elements with score equal to max or min).
- #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::CoreCommands
#exists?, #expire, #expireat, #marshal, #move, #persist, #rename, #renamenx, #sort, #ttl, #type, #unmarshal
Methods inherited from BaseObject
expiration_filter, #initialize, #redis, #set_expiration
Constructor Details
This class inherits a constructor from Redis::BaseObject
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
13 14 15 |
# File 'lib/redis/sorted_set.rb', line 13 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/redis/sorted_set.rb', line 13 def @options end |
Instance Method Details
#==(x) ⇒ Object
266 267 268 |
# File 'lib/redis/sorted_set.rb', line 266 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.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/redis/sorted_set.rb', line 42 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 |
#[]=(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
18 19 20 |
# File 'lib/redis/sorted_set.rb', line 18 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).
25 26 27 |
# File 'lib/redis/sorted_set.rb', line 25 def add(member, score) redis.zadd(key, score, marshal(member)) end |
#at(index) ⇒ Object
Return the value at the given index. Can also use familiar list syntax. Redis: ZRANGE
276 277 278 |
# File 'lib/redis/sorted_set.rb', line 276 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.
183 184 185 |
# File 'lib/redis/sorted_set.rb', line 183 def decrement(member, by=1) redis.zincrby(key, -by, marshal(member)).to_i end |
#delete(value) ⇒ Object
Delete the value from the set. Redis: ZREM
158 159 160 |
# File 'lib/redis/sorted_set.rb', line 158 def delete(value) redis.zrem(key, marshal(value)) end |
#delete_if(&block) ⇒ Object
Delete element if it matches block
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/redis/sorted_set.rb', line 163 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(unmarshal(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
248 249 250 |
# File 'lib/redis/sorted_set.rb', line 248 def difference(*sets) redis.zdiff(key, *keys_from_objects(sets)).map{|v| unmarshal(v) } 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
257 258 259 |
# File 'lib/redis/sorted_set.rb', line 257 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
262 263 264 |
# File 'lib/redis/sorted_set.rb', line 262 def empty? length == 0 end |
#first ⇒ Object
Return the first element in the list. Redis: ZRANGE(0)
281 282 283 |
# File 'lib/redis/sorted_set.rb', line 281 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
176 177 178 |
# File 'lib/redis/sorted_set.rb', line 176 def increment(member, by=1) redis.zincrby(key, by, marshal(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
200 201 202 |
# File 'lib/redis/sorted_set.rb', line 200 def intersection(*sets) redis.zinter(key, *keys_from_objects(sets)).map{|v| unmarshal(v) } 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
209 210 211 |
# File 'lib/redis/sorted_set.rb', line 209 def interstore(name, *sets) redis.zinterstore(name, keys_from_objects([self] + sets)) end |
#last ⇒ Object
Return the last element in the list. Redis: ZRANGE(-1)
286 287 288 |
# File 'lib/redis/sorted_set.rb', line 286 def last at(-1) end |
#length ⇒ Object Also known as: size
The number of members in the set. Aliased as size. Redis: ZCARD
291 292 293 |
# File 'lib/redis/sorted_set.rb', line 291 def length redis.zcard(key) end |
#member?(value) ⇒ Boolean
Return a boolean indicating whether value
is a member.
302 303 304 |
# File 'lib/redis/sorted_set.rb', line 302 def member?(value) !redis.zscore(key, marshal(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.
88 89 90 91 |
# File 'lib/redis/sorted_set.rb', line 88 def members(={}) vals = range(0, -1, ) vals.nil? ? [] : vals.map{|v| unmarshal(v) } end |
#merge(values) ⇒ Object Also known as: add_all
Add a list of members and their corresponding value (or a hash mapping values to scores) 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).
33 34 35 36 |
# File 'lib/redis/sorted_set.rb', line 33 def merge(values) vals = values.map{|v,s| [s, marshal(v)] } redis.zadd(key, vals) 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
95 96 97 98 99 100 101 |
# File 'lib/redis/sorted_set.rb', line 95 def range(start_index, end_index, ={}) if [:withscores] || [:with_scores] redis.zrange(key, start_index, end_index, :with_scores => true).map{|v,s| [unmarshal(v), s] } else redis.zrange(key, start_index, end_index).map{|v| unmarshal(v) } end end |
#range_size(min, max) ⇒ Object
The number of members within a range of scores. Redis: ZCOUNT
297 298 299 |
# File 'lib/redis/sorted_set.rb', line 297 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
117 118 119 120 121 122 123 124 |
# File 'lib/redis/sorted_set.rb', line 117 def rangebyscore(min, max, ={}) args = {} args[:limit] = [[:offset] || 0, [:limit] || [:count]] if [:offset] || [:limit] || [:count] args[:with_scores] = true if [:withscores] || [:with_scores] redis.zrangebyscore(key, min, max, args).map{|v| unmarshal(v) } 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
70 71 72 73 74 75 76 |
# File 'lib/redis/sorted_set.rb', line 70 def rank(member) if n = redis.zrank(key, marshal(member)) n.to_i else nil end 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
147 148 149 |
# File 'lib/redis/sorted_set.rb', line 147 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
153 154 155 |
# File 'lib/redis/sorted_set.rb', line 153 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
104 105 106 107 108 109 110 |
# File 'lib/redis/sorted_set.rb', line 104 def revrange(start_index, end_index, ={}) if [:withscores] || [:with_scores] redis.zrevrange(key, start_index, end_index, :with_scores => true).map{|v| unmarshal(v) } else redis.zrevrange(key, start_index, end_index).map{|v| unmarshal(v) } end end |
#revrangebyscore(max, min, options = {}) ⇒ Object
Returns all the elements in the sorted set at key with a score between max and min (including elements with score equal to max or min). In contrary to the default ordering of sorted sets, for this command the elements are considered to be ordered from high to low scores. Options:
:count, :offset - passed to LIMIT
:withscores - if true, scores are returned as well
Redis: ZREVRANGEBYSCORE
133 134 135 136 137 138 139 140 |
# File 'lib/redis/sorted_set.rb', line 133 def revrangebyscore(max, min, ={}) args = {} args[:limit] = [[:offset] || 0, [:limit] || [:count]] if [:offset] || [:limit] || [:count] args[:with_scores] = true if [:withscores] || [:with_scores] redis.zrevrangebyscore(key, max, min, args).map{|v| unmarshal(v) } end |
#revrank(member) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/redis/sorted_set.rb', line 78 def revrank(member) if n = redis.zrevrank(key, marshal(member)) n.to_i else nil end 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.
60 61 62 63 64 |
# File 'lib/redis/sorted_set.rb', line 60 def score(member) result = redis.zscore(key, marshal(member)) result.to_f unless result.nil? end |
#to_s ⇒ Object
270 271 272 |
# File 'lib/redis/sorted_set.rb', line 270 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
224 225 226 |
# File 'lib/redis/sorted_set.rb', line 224 def union(*sets) redis.zunion(key, *keys_from_objects(sets)).map{|v| unmarshal(v) } 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
232 233 234 |
# File 'lib/redis/sorted_set.rb', line 232 def unionstore(name, *sets) redis.zunionstore(name, keys_from_objects([self] + sets)) end |