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,234 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'redis'
|
3
|
+
|
4
|
+
class RedisCluster
|
5
|
+
module Function
|
6
|
+
|
7
|
+
# String implement redis strings commands. There will be some adjustment for cluster.
|
8
|
+
# see https://redis.io/commands#string. Most of the code are copied from
|
9
|
+
# https://github.com/redis/redis-rb/blob/master/lib/redis.rb.
|
10
|
+
#
|
11
|
+
# SETTER = [:getset, :append, :setbit, :setrange, :set, :setex, :psetex, :setnx, :incr,
|
12
|
+
# :incrby, :incrbyfloat, :decr, :decrby]
|
13
|
+
# GETTER = [:strlen, :bitpos, :bitcount, :getbit, :getrange, :get]
|
14
|
+
module String
|
15
|
+
|
16
|
+
# Decrement the integer value of a key by one.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# redis.decr("value")
|
20
|
+
# # => 4
|
21
|
+
#
|
22
|
+
# @param [String] key
|
23
|
+
# @return [Fixnum] value after decrementing it
|
24
|
+
def decr(key)
|
25
|
+
call(key, [:decr, key])
|
26
|
+
end
|
27
|
+
|
28
|
+
# Decrement the integer value of a key by the given number.
|
29
|
+
#
|
30
|
+
# @example
|
31
|
+
# redis.decrby("value", 5)
|
32
|
+
# # => 0
|
33
|
+
#
|
34
|
+
# @param [String] key
|
35
|
+
# @param [Fixnum] decrement
|
36
|
+
# @return [Fixnum] value after decrementing it
|
37
|
+
def decrby(key, decrement)
|
38
|
+
call(key, [:decrby, key, decrement])
|
39
|
+
end
|
40
|
+
|
41
|
+
# Increment the integer value of a key by one.
|
42
|
+
#
|
43
|
+
# @example
|
44
|
+
# redis.incr("value")
|
45
|
+
# # => 6
|
46
|
+
#
|
47
|
+
# @param [String] key
|
48
|
+
# @return [Fixnum] value after incrementing it
|
49
|
+
def incr(key)
|
50
|
+
call(key, [:incr, key])
|
51
|
+
end
|
52
|
+
|
53
|
+
# Increment the integer value of a key by the given integer number.
|
54
|
+
#
|
55
|
+
# @example
|
56
|
+
# redis.incrby("value", 5)
|
57
|
+
# # => 10
|
58
|
+
#
|
59
|
+
# @param [String] key
|
60
|
+
# @param [Fixnum] increment
|
61
|
+
# @return [Fixnum] value after incrementing it
|
62
|
+
def incrby(key, increment)
|
63
|
+
call(key, [:incrby, key, increment])
|
64
|
+
end
|
65
|
+
|
66
|
+
# Increment the numeric value of a key by the given float number.
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# redis.incrbyfloat("value", 1.23)
|
70
|
+
# # => 1.23
|
71
|
+
#
|
72
|
+
# @param [String] key
|
73
|
+
# @param [Float] increment
|
74
|
+
# @return [Float] value after incrementing it
|
75
|
+
def incrbyfloat(key, increment)
|
76
|
+
call(key, [:incrbyfloat, key, increment], transform: Redis::Floatify)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Set the string value of a key.
|
80
|
+
#
|
81
|
+
# @param [String] key
|
82
|
+
# @param [String] value
|
83
|
+
# @param [Hash] options
|
84
|
+
# - `:ex => Fixnum`: Set the specified expire time, in seconds.
|
85
|
+
# - `:px => Fixnum`: Set the specified expire time, in milliseconds.
|
86
|
+
# - `:nx => true`: Only set the key if it does not already exist.
|
87
|
+
# - `:xx => true`: Only set the key if it already exist.
|
88
|
+
# @return [String, Boolean] `"OK"` or true, false if `:nx => true` or `:xx => true`
|
89
|
+
def set(key, value, options = {})
|
90
|
+
ex = options[:ex]
|
91
|
+
px = options[:px]
|
92
|
+
args = [:set, key, value.to_s]
|
93
|
+
|
94
|
+
args.concat(['EX', ex]) if ex
|
95
|
+
args.concat(['PX', px]) if px
|
96
|
+
args.concat(['NX']) if options[:nx]
|
97
|
+
args.concat(['XX']) if options[:xx]
|
98
|
+
|
99
|
+
call(key, args, transform: Redis::BoolifySet)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Set the time to live in seconds of a key.
|
103
|
+
#
|
104
|
+
# @param [String] key
|
105
|
+
# @param [Fixnum] ttl
|
106
|
+
# @param [String] value
|
107
|
+
# @return [String] `"OK"`
|
108
|
+
def setex(key, ttl, value)
|
109
|
+
call(key, [:setex, key, ttl, value.to_s])
|
110
|
+
end
|
111
|
+
|
112
|
+
# Set the time to live in milliseconds of a key.
|
113
|
+
#
|
114
|
+
# @param [String] key
|
115
|
+
# @param [Fixnum] ttl
|
116
|
+
# @param [String] value
|
117
|
+
# @return [String] `"OK"`
|
118
|
+
def psetex(key, ttl, value)
|
119
|
+
call(key, [:psetex, key, ttl, value.to_s])
|
120
|
+
end
|
121
|
+
|
122
|
+
# Set the value of a key, only if the key does not exist.
|
123
|
+
#
|
124
|
+
# @param [String] key
|
125
|
+
# @param [String] value
|
126
|
+
# @return [Boolean] whether the key was set or not
|
127
|
+
def setnx(key, value)
|
128
|
+
call(key, [:setnx, key, value.to_s], transform: Redis::Boolify)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Get the value of a key.
|
132
|
+
#
|
133
|
+
# @param [String] key
|
134
|
+
# @return [String]
|
135
|
+
def get(key)
|
136
|
+
call(key, [:get, key], read: true)
|
137
|
+
end
|
138
|
+
|
139
|
+
# Overwrite part of a string at key starting at the specified offset.
|
140
|
+
#
|
141
|
+
# @param [String] key
|
142
|
+
# @param [Fixnum] offset byte offset
|
143
|
+
# @param [String] value
|
144
|
+
# @return [Fixnum] length of the string after it was modified
|
145
|
+
def setrange(key, offset, value)
|
146
|
+
call(key, [:setrange, key, offset, value.to_s])
|
147
|
+
end
|
148
|
+
|
149
|
+
# Get a substring of the string stored at a key.
|
150
|
+
#
|
151
|
+
# @param [String] key
|
152
|
+
# @param [Fixnum] start zero-based start offset
|
153
|
+
# @param [Fixnum] stop zero-based end offset. Use -1 for representing
|
154
|
+
# the end of the string
|
155
|
+
# @return [Fixnum] `0` or `1`
|
156
|
+
def getrange(key, start, stop)
|
157
|
+
call(key, [:getrange, key, start, stop], read: true)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Sets or clears the bit at offset in the string value stored at key.
|
161
|
+
#
|
162
|
+
# @param [String] key
|
163
|
+
# @param [Fixnum] offset bit offset
|
164
|
+
# @param [Fixnum] value bit value `0` or `1`
|
165
|
+
# @return [Fixnum] the original bit value stored at `offset`
|
166
|
+
def setbit(key, offset, value)
|
167
|
+
call(key, [:setbit, key, offset, value])
|
168
|
+
end
|
169
|
+
|
170
|
+
# Returns the bit value at offset in the string value stored at key.
|
171
|
+
#
|
172
|
+
# @param [String] key
|
173
|
+
# @param [Fixnum] offset bit offset
|
174
|
+
# @return [Fixnum] `0` or `1`
|
175
|
+
def getbit(key, offset)
|
176
|
+
call(key, [:getbit, key, offset], read: true)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Append a value to a key.
|
180
|
+
#
|
181
|
+
# @param [String] key
|
182
|
+
# @param [String] value value to append
|
183
|
+
# @return [Fixnum] length of the string after appending
|
184
|
+
def append(key, value)
|
185
|
+
call(key, [:append, key, value])
|
186
|
+
end
|
187
|
+
|
188
|
+
# Count the number of set bits in a range of the string value stored at key.
|
189
|
+
#
|
190
|
+
# @param [String] key
|
191
|
+
# @param [Fixnum] start start index
|
192
|
+
# @param [Fixnum] stop stop index
|
193
|
+
# @return [Fixnum] the number of bits set to 1
|
194
|
+
def bitcount(key, start = 0, stop = -1)
|
195
|
+
call(key, [:bitcount, key, start, stop], read: true)
|
196
|
+
end
|
197
|
+
|
198
|
+
# Return the position of the first bit set to 1 or 0 in a string.
|
199
|
+
#
|
200
|
+
# @param [String] key
|
201
|
+
# @param [Fixnum] bit whether to look for the first 1 or 0 bit
|
202
|
+
# @param [Fixnum] start start index
|
203
|
+
# @param [Fixnum] stop stop index
|
204
|
+
# @return [Fixnum] the position of the first 1/0 bit.
|
205
|
+
# -1 if looking for 1 and it is not found or start and stop are given.
|
206
|
+
def bitpos(key, bit, start = nil, stop = nil)
|
207
|
+
command = [:bitpos, key, bit]
|
208
|
+
command << start if start
|
209
|
+
command << stop if start && stop
|
210
|
+
|
211
|
+
call(key, command, read: true)
|
212
|
+
end
|
213
|
+
|
214
|
+
# Set the string value of a key and return its old value.
|
215
|
+
#
|
216
|
+
# @param [String] key
|
217
|
+
# @param [String] value value to replace the current value with
|
218
|
+
# @return [String] the old value stored in the key, or `nil` if the key
|
219
|
+
# did not exist
|
220
|
+
def getset(key, value)
|
221
|
+
call(key, [:getset, key, value.to_s])
|
222
|
+
end
|
223
|
+
|
224
|
+
# Get the length of the value stored in a key.
|
225
|
+
#
|
226
|
+
# @param [String] key
|
227
|
+
# @return [Fixnum] the length of the value stored in the key, or 0
|
228
|
+
# if the key does not exist
|
229
|
+
def strlen(key)
|
230
|
+
call(key, [:strlen, key], read: true)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'redis'
|
3
|
+
|
4
|
+
class RedisCluster
|
5
|
+
|
6
|
+
# Future basically act the same way as Redis::Future with some modification
|
7
|
+
class Future
|
8
|
+
attr_reader :command, :slot
|
9
|
+
attr_accessor :asking, :url
|
10
|
+
|
11
|
+
def initialize(slot, command, transformation)
|
12
|
+
@slot = slot
|
13
|
+
@command = command
|
14
|
+
@transformation = transformation
|
15
|
+
@value = Redis::Future::FutureNotReady
|
16
|
+
@asking = false
|
17
|
+
end
|
18
|
+
|
19
|
+
def value
|
20
|
+
raise @value if @value.is_a?(::RuntimeError)
|
21
|
+
@value
|
22
|
+
end
|
23
|
+
|
24
|
+
def value=(value)
|
25
|
+
@value = @transformation.call(value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-cluster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bukalapak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- sre@bukalapak.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- README.md
|
35
|
+
- lib/redis-cluster.rb
|
36
|
+
- lib/redis_cluster/client.rb
|
37
|
+
- lib/redis_cluster/cluster.rb
|
38
|
+
- lib/redis_cluster/function.rb
|
39
|
+
- lib/redis_cluster/function/hash.rb
|
40
|
+
- lib/redis_cluster/function/key.rb
|
41
|
+
- lib/redis_cluster/function/list.rb
|
42
|
+
- lib/redis_cluster/function/scan.rb
|
43
|
+
- lib/redis_cluster/function/set.rb
|
44
|
+
- lib/redis_cluster/function/sorted_set.rb
|
45
|
+
- lib/redis_cluster/function/string.rb
|
46
|
+
- lib/redis_cluster/future.rb
|
47
|
+
- lib/redis_cluster/version.rb
|
48
|
+
homepage: https://github.com/bukalapak/redis-cluster
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata: {}
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 2.5.1
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: Redis cluster client. Support pipelining.
|
72
|
+
test_files: []
|