class Redstruct::ConnectionProxy

Connection proxy class for the ConnectionPool

Constants

NON_COMMAND_METHODS

@return [Array<Symbol>] List of methods from the Redis class that we don't want to proxy

Public Class Methods

new(pool_or_conn) click to toggle source

@param [Redis, ConnectionPool<Redis>] pool_or_conn a redis connection, or a pool of redis connections @raise [ArgumentError] raises an exception if the argument is not one of the required classes

# File lib/redstruct/connection_proxy.rb, line 18
def initialize(pool_or_conn)
  case pool_or_conn
  when ConnectionPool
    @pool = pool_or_conn
  when Redis
    @redis = pool_or_conn
  else
    raise(ArgumentError, 'requires an instance of ConnectionPool or Redis to proxy to')
  end
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source

Fallback when calling methods we may not have dynamically created above @param [String, Symbol] method the called method name @param [Array<Object>] args the arguments it was called with @param [Proc] block optionally, the block it was called with @return [Object] whatever the method returns

Calls superclass method
# File lib/redstruct/connection_proxy.rb, line 100
def method_missing(method, *args, &block)
  with do |c|
    if c.respond_to?(method)
      c.public_send(method, *args, &block)
    else
      super
    end
  end
end
with() { |c| ... } click to toggle source

Executes the given block by first fixing a thread local connection from the pool, such that all redis commands executed within the block are on the same connection. This is necessary when doing pipelining, or multi/exec stuff @yield [Redis] a direct redis connection @return [Object] whatever the passed block evaluates to, nil otherwise

# File lib/redstruct/connection_proxy.rb, line 34
def with(&_block)
  unless block_given?
    Redstruct.logger.warn('do not Redstruct::ConnectionProxy#with with no block')
    return
  end

  connection = @redis || Thread.current[:__redstruct_connection]
  result = if connection.nil?
    @pool.with do |c|
      begin
        Thread.current[:__redstruct_connection] = c
        yield(c)
      ensure
        Thread.current[:__redstruct_connection] = nil
      end
    end
  else
    yield(connection)
  end

  return result
end
zlexcount(key, min, max) click to toggle source

see: redis.io/commands/zlexcount zlexcount is not supported as of redis-rb 3.3.2

# File lib/redstruct/connection_proxy.rb, line 76
def zlexcount(key, min, max)
  with do |c|
    c.synchronize do |client|
      client.call([:zlexcount, key, min, max])
    end
  end
end

Private Instance Methods

respond_to_missing?(_method, _include_private = false) click to toggle source

Necessary when overwriting method_missing, so that respond_to? work properly @param [String, Symbol] _method the method name @param [Boolean] _include_private if true, also looks up private methods @return [Boolean] true if responding through method_missing, false otherwise

# File lib/redstruct/connection_proxy.rb, line 90
def respond_to_missing?(_method, _include_private = false)
  true
end