Class: Blendris::RedisSortedSet
Overview
RedisSortedSet is a wrapper to the Redis ZSET data type.
Instance Attribute Summary
Attributes inherited from RedisNode
#key
Instance Method Summary
collapse
Methods inherited from RedisNode
#clear, #exists?, #notify_changed, #rename, #type
#generate_key, #in_temporary_set, redis, #redis
Methods included from Utils
#blank, #camelize, #constantize, #pairify, #sanitize_key
Constructor Details
#initialize(key, options = {}) ⇒ RedisSortedSet
Returns a new instance of RedisSortedSet.
9
10
11
12
13
|
# File 'lib/blendris/zset.rb', line 9
def initialize(key, options = {})
@key = key.to_s
@options = options
@on_change = options[:on_change]
end
|
Instance Method Details
#<<(pairs) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/blendris/zset.rb', line 57
def <<(pairs)
tempkey = "#{key}:::TEMP:::#{rand}"
redis.del tempkey
pairify(pairs).each do |score, value|
redis.zadd tempkey, score, value
end
redis.zunionstore key, [ key, tempkey ]
redis.del tempkey
self
ensure
notify_changed
end
|
#count ⇒ Object
103
104
105
|
# File 'lib/blendris/zset.rb', line 103
def count
redis.zcard key
end
|
#delete(value) ⇒ Object
77
78
79
80
81
|
# File 'lib/blendris/zset.rb', line 77
def delete(value)
redis.zrem key, value
ensure
notify_changed
end
|
#delete_by_score(min, max) ⇒ Object
83
84
85
86
87
|
# File 'lib/blendris/zset.rb', line 83
def delete_by_score(min, max)
redis.zremrangebyscore key, min, max
ensure
notify_changed
end
|
#each(min = "-inf", max = "+inf", mode = :score) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/blendris/zset.rb', line 15
def each(min = "-inf", max = "+inf", mode = :score)
case mode
when :rank then redis.zrange key, min, max
when :score then redis.zrangebyscore key, min, max
else raise "unknown zset mode #{mode}"
end.each do |value|
yield value
end
self
end
|
#each_with_scores(min = "-inf", max = "+inf", mode = :score) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/blendris/zset.rb', line 27
def each_with_scores(min = "-inf", max = "+inf", mode = :score)
flat_pairs =
case mode
when :rank then redis.zrange key, min, max, :with_scores => true
when :score then redis.zrangebyscore key, min, max, :with_scores => true
else raise "unknown zset mode #{mode}"
end
pairify(flat_pairs).each do |value, score|
yield score.to_f, value
end
self
end
|
#get ⇒ Object
73
74
75
|
# File 'lib/blendris/zset.rb', line 73
def get
self
end
|
#intersect!(other) ⇒ Object
Set this zset’s members to the intersection of this set and the given set.
90
91
92
93
94
|
# File 'lib/blendris/zset.rb', line 90
def intersect!(other)
redis.zinterstore key, [ key, other.key ]
ensure
notify_changed
end
|
#set(*pairs) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/blendris/zset.rb', line 42
def set(*pairs)
tempkey = "#{key}:::TEMP:::#{rand}"
redis.del tempkey
pairify(pairs).each do |score, value|
redis.zadd tempkey, score, value
end
redis.rename tempkey, key
self
ensure
notify_changed
end
|
#union!(other) ⇒ Object
Set this zset’s members to the union of this set and the given set.
97
98
99
100
101
|
# File 'lib/blendris/zset.rb', line 97
def union!(other)
redis.zunionstore key, [ key, other.key ]
ensure
notify_changed
end
|