redis-cluster 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +36 -0
- data/lib/redis-cluster.rb +208 -0
- data/lib/redis_cluster/client.rb +54 -0
- data/lib/redis_cluster/cluster.rb +179 -0
- data/lib/redis_cluster/function.rb +23 -0
- data/lib/redis_cluster/function/hash.rb +150 -0
- data/lib/redis_cluster/function/key.rb +107 -0
- data/lib/redis_cluster/function/list.rb +139 -0
- data/lib/redis_cluster/function/scan.rb +90 -0
- data/lib/redis_cluster/function/set.rb +83 -0
- data/lib/redis_cluster/function/sorted_set.rb +375 -0
- data/lib/redis_cluster/function/string.rb +234 -0
- data/lib/redis_cluster/future.rb +28 -0
- data/lib/redis_cluster/version.rb +5 -0
- metadata +72 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'redis'
|
3
|
+
|
4
|
+
class RedisCluster
|
5
|
+
module Function
|
6
|
+
|
7
|
+
# Set implement redis sets commands. There will be some adjustment for cluster.
|
8
|
+
# see https://redis.io/commands#set. Most of the code are copied from
|
9
|
+
# https://github.com/redis/redis-rb/blob/master/lib/redis.rb.
|
10
|
+
#
|
11
|
+
# SETTER = [:sadd, :spop, :srem]
|
12
|
+
# GETTER = [:scard, :sismembers, :smembers, :srandmember, :sscan]
|
13
|
+
module Set
|
14
|
+
|
15
|
+
# Get the number of members in a set.
|
16
|
+
#
|
17
|
+
# @param [String] key
|
18
|
+
# @return [Fixnum]
|
19
|
+
def scard(key)
|
20
|
+
call(key, [:scard, key], read: true)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Add one or more members to a set.
|
24
|
+
#
|
25
|
+
# @param [String] key
|
26
|
+
# @param [String, Array<String>] member one member, or array of members
|
27
|
+
# @return [Fixnum] number of members that were successfully added
|
28
|
+
def sadd(key, member)
|
29
|
+
call(key, [:sadd, key, member])
|
30
|
+
end
|
31
|
+
|
32
|
+
# Remove one or more members from a set.
|
33
|
+
#
|
34
|
+
# @param [String] key
|
35
|
+
# @param [String, Array<String>] member one member, or array of members
|
36
|
+
# @return [Boolean, Fixnum] number of members that were successfully removed
|
37
|
+
def srem(key, member)
|
38
|
+
call(key, [:srem, key, member])
|
39
|
+
end
|
40
|
+
|
41
|
+
# Remove and return one or more random member from a set.
|
42
|
+
#
|
43
|
+
# @param [String] key
|
44
|
+
# @return [String]
|
45
|
+
# @param [Fixnum] count
|
46
|
+
def spop(key, count = nil)
|
47
|
+
args = [:spop, key]
|
48
|
+
args << count if count
|
49
|
+
|
50
|
+
call(key, args)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Get one or more random members from a set.
|
54
|
+
#
|
55
|
+
# @param [String] key
|
56
|
+
# @param [Fixnum] count
|
57
|
+
# @return [String]
|
58
|
+
def srandmember(key, count = nil)
|
59
|
+
args = [:srandmember, key]
|
60
|
+
args << count if count
|
61
|
+
|
62
|
+
call(key, args, read: true)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Determine if a given value is a member of a set.
|
66
|
+
#
|
67
|
+
# @param [String] key
|
68
|
+
# @param [String] member
|
69
|
+
# @return [Boolean]
|
70
|
+
def sismember(key, member)
|
71
|
+
call(key, [:sismember, key, member], transform: Redis::Boolify, read: true)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Get all the members in a set.
|
75
|
+
#
|
76
|
+
# @param [String] key
|
77
|
+
# @return [Array<String>]
|
78
|
+
def smembers(key)
|
79
|
+
call(key, [:smembers, key], read: true)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,375 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'redis'
|
3
|
+
|
4
|
+
class RedisCluster
|
5
|
+
module Function
|
6
|
+
|
7
|
+
# SortedSet implement redis sorted set commands. There will be some adjustment for cluster.
|
8
|
+
# see https://redis.io/commands#sorted_set. Most of the code are copied from
|
9
|
+
# https://github.com/redis/redis-rb/blob/master/lib/redis.rb.
|
10
|
+
#
|
11
|
+
# SETTER = [:zadd, :zincrby, :zrem, :zremrangebyrank, :zremrangebyscore]
|
12
|
+
# GETTER = [:zcard, :zscore, :zrange, :zrevrange, :zrank, :zrevrange, :zrangebylex,
|
13
|
+
# :zrevrangebylex, :zrangebyscore, :zrevrangebyscore, :zcount]
|
14
|
+
module SortedSet
|
15
|
+
|
16
|
+
# Get the number of members in a sorted set.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# redis.zcard("zset")
|
20
|
+
# # => 4
|
21
|
+
#
|
22
|
+
# @param [String] key
|
23
|
+
# @return [Fixnum]
|
24
|
+
def zcard(key)
|
25
|
+
call(key, [:zcard, key], read: true)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Add one or more members to a sorted set, or update the score for members
|
29
|
+
# that already exist.
|
30
|
+
#
|
31
|
+
# @example Add a single `[score, member]` pair to a sorted set
|
32
|
+
# redis.zadd("zset", 32.0, "member")
|
33
|
+
# @example Add an array of `[score, member]` pairs to a sorted set
|
34
|
+
# redis.zadd("zset", [[32.0, "a"], [64.0, "b"]])
|
35
|
+
#
|
36
|
+
# @param [String] key
|
37
|
+
# @param [[Float, String], Array<[Float, String]>] args
|
38
|
+
# - a single `[score, member]` pair
|
39
|
+
# - an array of `[score, member]` pairs
|
40
|
+
# @param [Hash] options
|
41
|
+
# - `:xx => true`: Only update elements that already exist (never
|
42
|
+
# add elements)
|
43
|
+
# - `:nx => true`: Don't update already existing elements (always
|
44
|
+
# add new elements)
|
45
|
+
# - `:ch => true`: Modify the return value from the number of new
|
46
|
+
# elements added, to the total number of elements changed (CH is an
|
47
|
+
# abbreviation of changed); changed elements are new elements added
|
48
|
+
# and elements already existing for which the score was updated
|
49
|
+
# - `:incr => true`: When this option is specified ZADD acts like
|
50
|
+
# ZINCRBY; only one score-element pair can be specified in this mode
|
51
|
+
#
|
52
|
+
# @return [Boolean, Fixnum, Float]
|
53
|
+
# - `Boolean` when a single pair is specified, holding whether or not it was
|
54
|
+
# **added** to the sorted set.
|
55
|
+
# - `Fixnum` when an array of pairs is specified, holding the number of
|
56
|
+
# pairs that were **added** to the sorted set.
|
57
|
+
# - `Float` when option :incr is specified, holding the score of the member
|
58
|
+
# after incrementing it.
|
59
|
+
def zadd(key, *args)
|
60
|
+
zadd_options = [:zadd, key]
|
61
|
+
if args.last.is_a?(::Hash)
|
62
|
+
options = args.pop
|
63
|
+
incr = options[:incr]
|
64
|
+
|
65
|
+
zadd_options << 'NX' if options[:nx]
|
66
|
+
zadd_options << 'XX' if options[:xx]
|
67
|
+
zadd_options << 'CH' if options[:ch]
|
68
|
+
zadd_options << 'INCR' if incr
|
69
|
+
end
|
70
|
+
|
71
|
+
if args.size == 1 && args[0].is_a?(Array)
|
72
|
+
# Variadic: return float if INCR, integer if !INCR
|
73
|
+
call(key, zadd_options + args[0], transform: (incr ? Redis::Floatify : nil))
|
74
|
+
elsif args.size == 2
|
75
|
+
# Single pair: return float if INCR, boolean if !INCR
|
76
|
+
call(key, zadd_options + args, transform: (incr ? Redis::Floatify : Redis::Boolify))
|
77
|
+
else
|
78
|
+
raise ArgumentError, 'wrong number of arguments'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Increment the score of a member in a sorted set.
|
83
|
+
#
|
84
|
+
# @example
|
85
|
+
# redis.zincrby("zset", 32.0, "a")
|
86
|
+
# # => 64.0
|
87
|
+
#
|
88
|
+
# @param [String] key
|
89
|
+
# @param [Float] increment
|
90
|
+
# @param [String] member
|
91
|
+
# @return [Float] score of the member after incrementing it
|
92
|
+
def zincrby(key, increment, member)
|
93
|
+
call(key, [:zincrby, key, increment, member], transform: Redis::Floatify)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Remove one or more members from a sorted set.
|
97
|
+
#
|
98
|
+
# @example Remove a single member from a sorted set
|
99
|
+
# redis.zrem("zset", "a")
|
100
|
+
# @example Remove an array of members from a sorted set
|
101
|
+
# redis.zrem("zset", ["a", "b"])
|
102
|
+
#
|
103
|
+
# @param [String] key
|
104
|
+
# @param [String, Array<String>] member
|
105
|
+
# - a single member
|
106
|
+
# - an array of members
|
107
|
+
#
|
108
|
+
# @return [Fixnum] number of members that were removed to the sorted set
|
109
|
+
def zrem(key, member)
|
110
|
+
call(key, [:zrem, key, member])
|
111
|
+
end
|
112
|
+
|
113
|
+
# Get the score associated with the given member in a sorted set.
|
114
|
+
#
|
115
|
+
# @example Get the score for member "a"
|
116
|
+
# redis.zscore("zset", "a")
|
117
|
+
# # => 32.0
|
118
|
+
#
|
119
|
+
# @param [String] key
|
120
|
+
# @param [String] member
|
121
|
+
# @return [Float] score of the member
|
122
|
+
def zscore(key, member)
|
123
|
+
call(key, [:zscore, key, member], transform: Redis::Floatify, read: true)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Return a range of members in a sorted set, by index.
|
127
|
+
#
|
128
|
+
# @example Retrieve all members from a sorted set
|
129
|
+
# redis.zrange("zset", 0, -1)
|
130
|
+
# # => ["a", "b"]
|
131
|
+
# @example Retrieve all members and their scores from a sorted set
|
132
|
+
# redis.zrange("zset", 0, -1, :withscores => true)
|
133
|
+
# # => [["a", 32.0], ["b", 64.0]]
|
134
|
+
#
|
135
|
+
# @param [String] key
|
136
|
+
# @param [Fixnum] start start index
|
137
|
+
# @param [Fixnum] stop stop index
|
138
|
+
# @param [Hash] options
|
139
|
+
# - `:withscores => true`: include scores in output
|
140
|
+
#
|
141
|
+
# @return [Array<String>, Array<[String, Float]>]
|
142
|
+
# - when `:withscores` is not specified, an array of members
|
143
|
+
# - when `:withscores` is specified, an array with `[member, score]` pairs
|
144
|
+
def zrange(key, start, stop, options = {})
|
145
|
+
args = [:zrange, key, start, stop]
|
146
|
+
|
147
|
+
if options[:withscores]
|
148
|
+
args << 'WITHSCORES'
|
149
|
+
block = Redis::FloatifyPairs
|
150
|
+
end
|
151
|
+
|
152
|
+
call(key, args, transform: block, read: true)
|
153
|
+
end
|
154
|
+
|
155
|
+
# Return a range of members in a sorted set, by index, with scores ordered
|
156
|
+
# from high to low.
|
157
|
+
#
|
158
|
+
# @example Retrieve all members from a sorted set
|
159
|
+
# redis.zrevrange("zset", 0, -1)
|
160
|
+
# # => ["b", "a"]
|
161
|
+
# @example Retrieve all members and their scores from a sorted set
|
162
|
+
# redis.zrevrange("zset", 0, -1, :withscores => true)
|
163
|
+
# # => [["b", 64.0], ["a", 32.0]]
|
164
|
+
#
|
165
|
+
# @see #zrange
|
166
|
+
def zrevrange(key, start, stop, options = {})
|
167
|
+
args = [:zrevrange, key, start, stop]
|
168
|
+
|
169
|
+
if options[:withscores]
|
170
|
+
args << 'WITHSCORES'
|
171
|
+
block = Redis::FloatifyPairs
|
172
|
+
end
|
173
|
+
|
174
|
+
call(key, args, transform: block, read: true)
|
175
|
+
end
|
176
|
+
|
177
|
+
# Determine the index of a member in a sorted set.
|
178
|
+
#
|
179
|
+
# @param [String] key
|
180
|
+
# @param [String] member
|
181
|
+
# @return [Fixnum]
|
182
|
+
def zrank(key, member)
|
183
|
+
call(key, [:zrank, key, member], read: true)
|
184
|
+
end
|
185
|
+
|
186
|
+
# Determine the index of a member in a sorted set, with scores ordered from
|
187
|
+
# high to low.
|
188
|
+
#
|
189
|
+
# @param [String] key
|
190
|
+
# @param [String] member
|
191
|
+
# @return [Fixnum]
|
192
|
+
def zrevrank(key, member)
|
193
|
+
call(key, [:zrevrank, key, member], read: true)
|
194
|
+
end
|
195
|
+
|
196
|
+
# Remove all members in a sorted set within the given indexes.
|
197
|
+
#
|
198
|
+
# @example Remove first 5 members
|
199
|
+
# redis.zremrangebyrank("zset", 0, 4)
|
200
|
+
# # => 5
|
201
|
+
# @example Remove last 5 members
|
202
|
+
# redis.zremrangebyrank("zset", -5, -1)
|
203
|
+
# # => 5
|
204
|
+
#
|
205
|
+
# @param [String] key
|
206
|
+
# @param [Fixnum] start start index
|
207
|
+
# @param [Fixnum] stop stop index
|
208
|
+
# @return [Fixnum] number of members that were removed
|
209
|
+
def zremrangebyrank(key, start, stop)
|
210
|
+
call(key, [:zremrangebyrank, key, start, stop])
|
211
|
+
end
|
212
|
+
|
213
|
+
# Return a range of members with the same score in a sorted set, by lexicographical ordering
|
214
|
+
#
|
215
|
+
# @example Retrieve members matching a
|
216
|
+
# redis.zrangebylex("zset", "[a", "[a\xff")
|
217
|
+
# # => ["aaren", "aarika", "abagael", "abby"]
|
218
|
+
# @example Retrieve the first 2 members matching a
|
219
|
+
# redis.zrangebylex("zset", "[a", "[a\xff", :limit => [0, 2])
|
220
|
+
# # => ["aaren", "aarika"]
|
221
|
+
#
|
222
|
+
# @param [String] key
|
223
|
+
# @param [String] min
|
224
|
+
# - inclusive minimum is specified by prefixing `(`
|
225
|
+
# - exclusive minimum is specified by prefixing `[`
|
226
|
+
# @param [String] max
|
227
|
+
# - inclusive maximum is specified by prefixing `(`
|
228
|
+
# - exclusive maximum is specified by prefixing `[`
|
229
|
+
# @param [Hash] options
|
230
|
+
# - `:limit => [offset, count]`: skip `offset` members, return a maximum of
|
231
|
+
# `count` members
|
232
|
+
#
|
233
|
+
# @return [Array<String>, Array<[String, Float]>]
|
234
|
+
def zrangebylex(key, min, max, options = {})
|
235
|
+
args = [:zrangebylex, key, min, max]
|
236
|
+
|
237
|
+
limit = options[:limit]
|
238
|
+
args.concat(['LIMIT'] + limit) if limit
|
239
|
+
|
240
|
+
call(key, args, read: true)
|
241
|
+
end
|
242
|
+
|
243
|
+
# Return a range of members with the same score in a sorted set, by reversed lexicographical
|
244
|
+
# ordering. Apart from the reversed ordering, #zrevrangebylex is similar to #zrangebylex.
|
245
|
+
#
|
246
|
+
# @example Retrieve members matching a
|
247
|
+
# redis.zrevrangebylex("zset", "[a", "[a\xff")
|
248
|
+
# # => ["abbygail", "abby", "abagael", "aaren"]
|
249
|
+
# @example Retrieve the last 2 members matching a
|
250
|
+
# redis.zrevrangebylex("zset", "[a", "[a\xff", :limit => [0, 2])
|
251
|
+
# # => ["abbygail", "abby"]
|
252
|
+
#
|
253
|
+
# @see #zrangebylex
|
254
|
+
def zrevrangebylex(key, max, min, options = {})
|
255
|
+
args = [:zrevrangebylex, key, min, max]
|
256
|
+
|
257
|
+
limit = options[:limit]
|
258
|
+
args.concat(['LIMIT'] + limit) if limit
|
259
|
+
|
260
|
+
call(key, args, read: true)
|
261
|
+
end
|
262
|
+
|
263
|
+
# Return a range of members in a sorted set, by score.
|
264
|
+
#
|
265
|
+
# @example Retrieve members with score `>= 5` and `< 100`
|
266
|
+
# redis.zrangebyscore("zset", "5", "(100")
|
267
|
+
# # => ["a", "b"]
|
268
|
+
# @example Retrieve the first 2 members with score `>= 0`
|
269
|
+
# redis.zrangebyscore("zset", "0", "+inf", :limit => [0, 2])
|
270
|
+
# # => ["a", "b"]
|
271
|
+
# @example Retrieve members and their scores with scores `> 5`
|
272
|
+
# redis.zrangebyscore("zset", "(5", "+inf", :withscores => true)
|
273
|
+
# # => [["a", 32.0], ["b", 64.0]]
|
274
|
+
#
|
275
|
+
# @param [String] key
|
276
|
+
# @param [String] min
|
277
|
+
# - inclusive minimum score is specified verbatim
|
278
|
+
# - exclusive minimum score is specified by prefixing `(`
|
279
|
+
# @param [String] max
|
280
|
+
# - inclusive maximum score is specified verbatim
|
281
|
+
# - exclusive maximum score is specified by prefixing `(`
|
282
|
+
# @param [Hash] options
|
283
|
+
# - `:withscores => true`: include scores in output
|
284
|
+
# - `:limit => [offset, count]`: skip `offset` members, return a maximum of
|
285
|
+
# `count` members
|
286
|
+
#
|
287
|
+
# @return [Array<String>, Array<[String, Float]>]
|
288
|
+
# - when `:withscores` is not specified, an array of members
|
289
|
+
# - when `:withscores` is specified, an array with `[member, score]` pairs
|
290
|
+
def zrangebyscore(key, min, max, options = {})
|
291
|
+
args = [:zrangebyscore, key, min, max]
|
292
|
+
|
293
|
+
if options[:withscores]
|
294
|
+
args << 'WITHSCORES'
|
295
|
+
block = Redis::FloatifyPairs
|
296
|
+
end
|
297
|
+
|
298
|
+
limit = options[:limit]
|
299
|
+
args.concat(['LIMIT'] + limit) if limit
|
300
|
+
|
301
|
+
call(key, args, transform: block, read: true)
|
302
|
+
end
|
303
|
+
|
304
|
+
# Return a range of members in a sorted set, by score, with scores ordered
|
305
|
+
# from high to low.
|
306
|
+
#
|
307
|
+
# @example Retrieve members with score `< 100` and `>= 5`
|
308
|
+
# redis.zrevrangebyscore("zset", "(100", "5")
|
309
|
+
# # => ["b", "a"]
|
310
|
+
# @example Retrieve the first 2 members with score `<= 0`
|
311
|
+
# redis.zrevrangebyscore("zset", "0", "-inf", :limit => [0, 2])
|
312
|
+
# # => ["b", "a"]
|
313
|
+
# @example Retrieve members and their scores with scores `> 5`
|
314
|
+
# redis.zrevrangebyscore("zset", "+inf", "(5", :withscores => true)
|
315
|
+
# # => [["b", 64.0], ["a", 32.0]]
|
316
|
+
#
|
317
|
+
# @see #zrangebyscore
|
318
|
+
def zrevrangebyscore(key, max, min, options = {})
|
319
|
+
args = [:zrevrangebyscore, key, min, max]
|
320
|
+
|
321
|
+
if options[:withscores]
|
322
|
+
args << 'WITHSCORES'
|
323
|
+
block = Redis::FloatifyPairs
|
324
|
+
end
|
325
|
+
|
326
|
+
limit = options[:limit]
|
327
|
+
args.concat(['LIMIT'] + limit) if limit
|
328
|
+
|
329
|
+
call(key, args, transform: block, read: true)
|
330
|
+
end
|
331
|
+
|
332
|
+
# Remove all members in a sorted set within the given scores.
|
333
|
+
#
|
334
|
+
# @example Remove members with score `>= 5` and `< 100`
|
335
|
+
# redis.zremrangebyscore("zset", "5", "(100")
|
336
|
+
# # => 2
|
337
|
+
# @example Remove members with scores `> 5`
|
338
|
+
# redis.zremrangebyscore("zset", "(5", "+inf")
|
339
|
+
# # => 2
|
340
|
+
#
|
341
|
+
# @param [String] key
|
342
|
+
# @param [String] min
|
343
|
+
# - inclusive minimum score is specified verbatim
|
344
|
+
# - exclusive minimum score is specified by prefixing `(`
|
345
|
+
# @param [String] max
|
346
|
+
# - inclusive maximum score is specified verbatim
|
347
|
+
# - exclusive maximum score is specified by prefixing `(`
|
348
|
+
# @return [Fixnum] number of members that were removed
|
349
|
+
def zremrangebyscore(key, min, max)
|
350
|
+
call(key, [:zremrangebyscore, key, min, max])
|
351
|
+
end
|
352
|
+
|
353
|
+
# Count the members in a sorted set with scores within the given values.
|
354
|
+
#
|
355
|
+
# @example Count members with score `>= 5` and `< 100`
|
356
|
+
# redis.zcount("zset", "5", "(100")
|
357
|
+
# # => 2
|
358
|
+
# @example Count members with scores `> 5`
|
359
|
+
# redis.zcount("zset", "(5", "+inf")
|
360
|
+
# # => 2
|
361
|
+
#
|
362
|
+
# @param [String] key
|
363
|
+
# @param [String] min
|
364
|
+
# - inclusive minimum score is specified verbatim
|
365
|
+
# - exclusive minimum score is specified by prefixing `(`
|
366
|
+
# @param [String] max
|
367
|
+
# - inclusive maximum score is specified verbatim
|
368
|
+
# - exclusive maximum score is specified by prefixing `(`
|
369
|
+
# @return [Fixnum] number of members in within the specified range
|
370
|
+
def zcount(key, min, max)
|
371
|
+
call(key, [:zcount, key, min, max], read: true)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
375
|
+
end
|