Class: RedisClient

Inherits:
Object show all
Includes:
Common
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/config.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/pooled.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/version.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/decorator.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/middlewares.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/circuit_breaker.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/command_builder.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/ruby_connection.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/connection_mixin.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/ruby_connection/resp3.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/ruby_connection/buffered_io.rb

Direct Known Subclasses

Redis::Client

Defined Under Namespace

Modules: CommandBuilder, Common, ConnectionMixin, Decorator, HasCommand, RESP3 Classes: BasicMiddleware, CircuitBreaker, CommandError, Config, Middlewares, Multi, Pipeline, Pooled, PubSub, RubyConnection, SentinelConfig

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.12.0"

Instance Attribute Summary

Attributes included from Common

#config, #connect_timeout, #id, #read_timeout, #write_timeout

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ RedisClient

Returns a new instance of RedisClient.



164
165
166
167
168
169
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 164

def initialize(config, **)
  super
  @middlewares = config.middlewares_stack.new(self)
  @raw_connection = nil
  @disable_reconnection = false
end

Class Method Details

.config(**kwargs) ⇒ Object



141
142
143
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 141

def config(**kwargs)
  Config.new(client_implementation: self, **kwargs)
end

.default_driverObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 31

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



43
44
45
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 43

def default_driver=(name)
  @default_driver = driver(name)
end

.driver(name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 20

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



149
150
151
152
153
154
155
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 149

def new(arg = nil, **kwargs)
  if arg.is_a?(Config::Common)
    super
  else
    super(config(**(arg || {}), **kwargs))
  end
end

.register(middleware) ⇒ Object



157
158
159
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 157

def register(middleware)
  Middlewares.include(middleware)
end

.register_driver(name, &block) ⇒ Object



16
17
18
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 16

def register_driver(name, &block)
  @driver_definitions[name] = block
end

.sentinel(**kwargs) ⇒ Object



145
146
147
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 145

def sentinel(**kwargs)
  SentinelConfig.new(client_implementation: self, **kwargs)
end

Instance Method Details

#blocking_call(timeout, *command, **kwargs) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 266

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



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 286

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 206

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 236

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



251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 251

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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 221

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

#closeObject



346
347
348
349
350
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 346

def close
  @raw_connection&.close
  @raw_connection = nil
  self
end

#connected?Boolean

Returns:

  • (Boolean)


342
343
344
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 342

def connected?
  @raw_connection&.connected?
end

#hscan(key, *args, **kwargs, &block) ⇒ Object



324
325
326
327
328
329
330
331
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 324

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

#inspectObject



171
172
173
174
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 171

def inspect
  id_string = " id=#{id}" if id
  "#<#{self.class.name} #{config.server_url}#{id_string}>"
end

#multi(watch: nil, &block) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 370

def multi(watch: nil, &block)
  transaction = nil

  results = if watch
    # WATCH is stateful, so we can't reconnect if it's used, the whole transaction
    # has to be redone.
    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

#pipelined {|pipeline| ... } ⇒ Object

Yields:

  • (pipeline)


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 352

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

#pubsubObject



200
201
202
203
204
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 200

def pubsub
  sub = PubSub.new(ensure_connected, @command_builder)
  @raw_connection = nil
  sub
end

#read_timeout=(timeout) ⇒ Object



190
191
192
193
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 190

def read_timeout=(timeout)
  super
  raw_connection.read_timeout = timeout if connected?
end

#scan(*args, **kwargs, &block) ⇒ Object



306
307
308
309
310
311
312
313
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 306

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

#sizeObject



176
177
178
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 176

def size
  1
end

#sscan(key, *args, **kwargs, &block) ⇒ Object



315
316
317
318
319
320
321
322
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 315

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=(timeout) ⇒ Object



185
186
187
188
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 185

def timeout=(timeout)
  super
  raw_connection.read_timeout = raw_connection.write_timeout = timeout if connected?
end

#with(_options = nil) {|_self| ... } ⇒ Object Also known as: then

Yields:

  • (_self)

Yield Parameters:

  • _self (RedisClient)

    the object that the method was called on



180
181
182
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 180

def with(_options = nil)
  yield self
end

#write_timeout=(timeout) ⇒ Object



195
196
197
198
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 195

def write_timeout=(timeout)
  super
  raw_connection.write_timeout = timeout if connected?
end

#zscan(key, *args, **kwargs, &block) ⇒ Object



333
334
335
336
337
338
339
340
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client.rb', line 333

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