Class: RedisClient
- Inherits:
-
Object
show all
- Includes:
- Common
- Defined in:
- lib/redis_client.rb,
lib/redis_client/config.rb,
lib/redis_client/pooled.rb,
lib/redis_client/version.rb,
lib/redis_client/decorator.rb,
lib/redis_client/pid_cache.rb,
lib/redis_client/url_config.rb,
lib/redis_client/middlewares.rb,
lib/redis_client/circuit_breaker.rb,
lib/redis_client/command_builder.rb,
lib/redis_client/ruby_connection.rb,
lib/redis_client/sentinel_config.rb,
lib/redis_client/connection_mixin.rb,
lib/redis_client/ruby_connection/resp3.rb,
lib/redis_client/ruby_connection/buffered_io.rb
Defined Under Namespace
Modules: CommandBuilder, Common, ConnectionMixin, Decorator, HasCommand, PIDCache, RESP3
Classes: BasicMiddleware, CircuitBreaker, CommandError, Config, Middlewares, Multi, Pipeline, Pooled, PubSub, RubyConnection, SentinelConfig, URLConfig
Constant Summary
collapse
- Error =
Class.new(StandardError)
- ProtocolError =
Class.new(Error)
- UnsupportedServer =
Class.new(Error)
- ConnectionError =
Class.new(Error)
- CannotConnectError =
Class.new(ConnectionError)
- FailoverError =
Class.new(ConnectionError)
- TimeoutError =
Class.new(ConnectionError)
- ReadTimeoutError =
Class.new(TimeoutError)
- WriteTimeoutError =
Class.new(TimeoutError)
- CheckoutTimeoutError =
Class.new(TimeoutError)
- AuthenticationError =
Class.new(CommandError)
- PermissionError =
Class.new(CommandError)
- WrongTypeError =
Class.new(CommandError)
- OutOfMemoryError =
Class.new(CommandError)
- ReadOnlyError =
Class.new(ConnectionError)
- MasterDownError =
Class.new(ConnectionError)
- VERSION =
"0.19.1"
Instance Attribute Summary
Attributes included from Common
#config, #connect_timeout, #read_timeout, #write_timeout
Class Method Summary
collapse
Instance Method Summary
collapse
-
#blocking_call(timeout, *command, **kwargs) ⇒ Object
-
#blocking_call_v(timeout, command) ⇒ Object
-
#call(*command, **kwargs) ⇒ Object
-
#call_once(*command, **kwargs) ⇒ Object
-
#call_once_v(command) ⇒ Object
-
#call_v(command) ⇒ Object
-
#close ⇒ Object
-
#connected? ⇒ Boolean
-
#db ⇒ Object
-
#disable_reconnection(&block) ⇒ Object
-
#host ⇒ Object
-
#hscan(key, *args, **kwargs, &block) ⇒ Object
-
#id ⇒ Object
-
#initialize(config) ⇒ RedisClient
constructor
A new instance of RedisClient.
-
#inspect ⇒ Object
-
#measure_round_trip_delay ⇒ Object
-
#multi(watch: nil, &block) ⇒ Object
-
#password ⇒ Object
-
#path ⇒ Object
-
#pipelined {|pipeline| ... } ⇒ Object
-
#port ⇒ Object
-
#pubsub ⇒ Object
-
#read_timeout=(timeout) ⇒ Object
-
#scan(*args, **kwargs, &block) ⇒ Object
-
#server_url ⇒ Object
-
#size ⇒ Object
-
#sscan(key, *args, **kwargs, &block) ⇒ Object
-
#timeout ⇒ Object
-
#timeout=(timeout) ⇒ Object
-
#username ⇒ Object
-
#with(_options = nil) {|_self| ... } ⇒ Object
(also: #then)
-
#write_timeout=(timeout) ⇒ Object
-
#zscan(key, *args, **kwargs, &block) ⇒ Object
Constructor Details
Returns a new instance of RedisClient.
166
167
168
169
170
171
|
# File 'lib/redis_client.rb', line 166
def initialize(config, **)
super
@middlewares = config.middlewares_stack.new(self)
@raw_connection = nil
@disable_reconnection = false
end
|
Class Method Details
.config(**kwargs) ⇒ Object
143
144
145
|
# File 'lib/redis_client.rb', line 143
def config(**kwargs)
Config.new(client_implementation: self, **kwargs)
end
|
.default_driver ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/redis_client.rb', line 33
def default_driver
unless @default_driver
@driver_definitions.each_key do |name|
if @default_driver = driver(name)
break
end
rescue LoadError
end
end
@default_driver
end
|
.default_driver=(name) ⇒ Object
45
46
47
|
# File 'lib/redis_client.rb', line 45
def default_driver=(name)
@default_driver = driver(name)
end
|
.driver(name) ⇒ Object
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/redis_client.rb', line 22
def driver(name)
return name if name.is_a?(Class)
name = name.to_sym
unless @driver_definitions.key?(name)
raise ArgumentError, "Unknown driver #{name.inspect}, expected one of: `#{@driver_definitions.keys.inspect}`"
end
@drivers[name] ||= @driver_definitions[name]&.call
end
|
.new(arg = nil, **kwargs) ⇒ Object
151
152
153
154
155
156
157
|
# File 'lib/redis_client.rb', line 151
def new(arg = nil, **kwargs)
if arg.is_a?(Config::Common)
super
else
super(config(**(arg || {}), **kwargs))
end
end
|
.register(middleware) ⇒ Object
159
160
161
|
# File 'lib/redis_client.rb', line 159
def register(middleware)
Middlewares.include(middleware)
end
|
.register_driver(name, &block) ⇒ Object
18
19
20
|
# File 'lib/redis_client.rb', line 18
def register_driver(name, &block)
@driver_definitions[name] = block
end
|
.sentinel(**kwargs) ⇒ Object
147
148
149
|
# File 'lib/redis_client.rb', line 147
def sentinel(**kwargs)
SentinelConfig.new(client_implementation: self, **kwargs)
end
|
Instance Method Details
#blocking_call(timeout, *command, **kwargs) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
# File 'lib/redis_client.rb', line 313
def blocking_call(timeout, *command, **kwargs)
command = @command_builder.generate(command, kwargs)
error = nil
result = ensure_connected do |connection|
@middlewares.call(command, config) do
connection.call(command, timeout)
end
rescue ReadTimeoutError => error
break
end
if error
raise error
elsif block_given?
yield result
else
result
end
end
|
#blocking_call_v(timeout, command) ⇒ Object
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'lib/redis_client.rb', line 333
def blocking_call_v(timeout, command)
command = @command_builder.generate(command)
error = nil
result = ensure_connected do |connection|
@middlewares.call(command, config) do
connection.call(command, timeout)
end
rescue ReadTimeoutError => error
break
end
if error
raise error
elsif block_given?
yield result
else
result
end
end
|
#call(*command, **kwargs) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/redis_client.rb', line 253
def call(*command, **kwargs)
command = @command_builder.generate(command, kwargs)
result = ensure_connected do |connection|
@middlewares.call(command, config) do
connection.call(command, nil)
end
end
if block_given?
yield result
else
result
end
end
|
#call_once(*command, **kwargs) ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/redis_client.rb', line 283
def call_once(*command, **kwargs)
command = @command_builder.generate(command, kwargs)
result = ensure_connected(retryable: false) do |connection|
@middlewares.call(command, config) do
connection.call(command, nil)
end
end
if block_given?
yield result
else
result
end
end
|
#call_once_v(command) ⇒ Object
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/redis_client.rb', line 298
def call_once_v(command)
command = @command_builder.generate(command)
result = ensure_connected(retryable: false) do |connection|
@middlewares.call(command, config) do
connection.call(command, nil)
end
end
if block_given?
yield result
else
result
end
end
|
#call_v(command) ⇒ Object
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# File 'lib/redis_client.rb', line 268
def call_v(command)
command = @command_builder.generate(command)
result = ensure_connected do |connection|
@middlewares.call(command, config) do
connection.call(command, nil)
end
end
if block_given?
yield result
else
result
end
end
|
#close ⇒ Object
393
394
395
396
|
# File 'lib/redis_client.rb', line 393
def close
@raw_connection&.close
self
end
|
#connected? ⇒ Boolean
389
390
391
|
# File 'lib/redis_client.rb', line 389
def connected?
@raw_connection&.revalidate
end
|
#db ⇒ Object
190
191
192
|
# File 'lib/redis_client.rb', line 190
def db
config.db
end
|
#disable_reconnection(&block) ⇒ Object
398
399
400
|
# File 'lib/redis_client.rb', line 398
def disable_reconnection(&block)
ensure_connected(retryable: false, &block)
end
|
#host ⇒ Object
194
195
196
|
# File 'lib/redis_client.rb', line 194
def host
config.host unless config.path
end
|
#hscan(key, *args, **kwargs, &block) ⇒ Object
371
372
373
374
375
376
377
378
|
# File 'lib/redis_client.rb', line 371
def hscan(key, *args, **kwargs, &block)
unless block_given?
return to_enum(__callee__, key, *args, **kwargs)
end
args = @command_builder.generate(["HSCAN", key, 0] + args, kwargs)
scan_pairs(2, args, &block)
end
|
#id ⇒ Object
182
183
184
|
# File 'lib/redis_client.rb', line 182
def id
config.id
end
|
#inspect ⇒ Object
173
174
175
176
|
# File 'lib/redis_client.rb', line 173
def inspect
id_string = " id=#{id}" if id
"#<#{self.class.name} #{config.server_url}#{id_string}>"
end
|
#measure_round_trip_delay ⇒ Object
245
246
247
248
249
250
251
|
# File 'lib/redis_client.rb', line 245
def measure_round_trip_delay
ensure_connected do |connection|
@middlewares.call(["PING"], config) do
connection.measure_round_trip_delay
end
end
end
|
#multi(watch: nil, &block) ⇒ Object
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
# File 'lib/redis_client.rb', line 420
def multi(watch: nil, &block)
transaction = nil
results = if watch
ensure_connected(retryable: false) do |connection|
call("WATCH", *watch)
begin
if transaction = build_transaction(&block)
commands = transaction._commands
results = @middlewares.call_pipelined(commands, config) do
connection.call_pipelined(commands, nil)
end.last
else
call("UNWATCH")
[]
end
rescue
call("UNWATCH") if connected? && watch
raise
end
end
else
transaction = build_transaction(&block)
if transaction._empty?
[]
else
ensure_connected(retryable: transaction._retryable?) do |connection|
commands = transaction._commands
@middlewares.call_pipelined(commands, config) do
connection.call_pipelined(commands, nil)
end.last
end
end
end
if transaction
transaction._coerce!(results)
else
results
end
end
|
#password ⇒ Object
210
211
212
|
# File 'lib/redis_client.rb', line 210
def password
config.password
end
|
#path ⇒ Object
202
203
204
|
# File 'lib/redis_client.rb', line 202
def path
config.path
end
|
#pipelined {|pipeline| ... } ⇒ Object
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
# File 'lib/redis_client.rb', line 402
def pipelined
pipeline = Pipeline.new(@command_builder)
yield pipeline
if pipeline._size == 0
[]
else
results = ensure_connected(retryable: pipeline._retryable?) do |connection|
commands = pipeline._commands
@middlewares.call_pipelined(commands, config) do
connection.call_pipelined(commands, pipeline._timeouts)
end
end
pipeline._coerce!(results)
end
end
|
#port ⇒ Object
198
199
200
|
# File 'lib/redis_client.rb', line 198
def port
config.port unless config.path
end
|
#pubsub ⇒ Object
239
240
241
242
243
|
# File 'lib/redis_client.rb', line 239
def pubsub
sub = PubSub.new(ensure_connected, @command_builder)
@raw_connection = nil
sub
end
|
#read_timeout=(timeout) ⇒ Object
229
230
231
232
|
# File 'lib/redis_client.rb', line 229
def read_timeout=(timeout)
super
@raw_connection&.read_timeout = timeout
end
|
#scan(*args, **kwargs, &block) ⇒ Object
353
354
355
356
357
358
359
360
|
# File 'lib/redis_client.rb', line 353
def scan(*args, **kwargs, &block)
unless block_given?
return to_enum(__callee__, *args, **kwargs)
end
args = @command_builder.generate(["SCAN", 0] + args, kwargs)
scan_list(1, args, &block)
end
|
#server_url ⇒ Object
178
179
180
|
# File 'lib/redis_client.rb', line 178
def server_url
config.server_url
end
|
#size ⇒ Object
214
215
216
|
# File 'lib/redis_client.rb', line 214
def size
1
end
|
#sscan(key, *args, **kwargs, &block) ⇒ Object
362
363
364
365
366
367
368
369
|
# File 'lib/redis_client.rb', line 362
def sscan(key, *args, **kwargs, &block)
unless block_given?
return to_enum(__callee__, key, *args, **kwargs)
end
args = @command_builder.generate(["SSCAN", key, 0] + args, kwargs)
scan_list(2, args, &block)
end
|
#timeout ⇒ Object
186
187
188
|
# File 'lib/redis_client.rb', line 186
def timeout
config.read_timeout
end
|
#timeout=(timeout) ⇒ Object
223
224
225
226
227
|
# File 'lib/redis_client.rb', line 223
def timeout=(timeout)
super
@raw_connection&.read_timeout = timeout
@raw_connection&.write_timeout = timeout
end
|
#username ⇒ Object
206
207
208
|
# File 'lib/redis_client.rb', line 206
def username
config.username
end
|
#with(_options = nil) {|_self| ... } ⇒ Object
Also known as:
then
218
219
220
|
# File 'lib/redis_client.rb', line 218
def with(_options = nil)
yield self
end
|
#write_timeout=(timeout) ⇒ Object
234
235
236
237
|
# File 'lib/redis_client.rb', line 234
def write_timeout=(timeout)
super
@raw_connection&.write_timeout = timeout
end
|
#zscan(key, *args, **kwargs, &block) ⇒ Object
380
381
382
383
384
385
386
387
|
# File 'lib/redis_client.rb', line 380
def zscan(key, *args, **kwargs, &block)
unless block_given?
return to_enum(__callee__, key, *args, **kwargs)
end
args = @command_builder.generate(["ZSCAN", key, 0] + args, kwargs)
scan_pairs(2, args, &block)
end
|