Class: Redstruct::ConnectionProxy

Inherits:
Object
  • Object
show all
Includes:
Utils::Inspectable
Defined in:
lib/redstruct/connection_proxy.rb

Overview

Connection proxy class for the ConnectionPool

Constant Summary

NON_COMMAND_METHODS =

Returns List of methods from the Redis class that we don't want to proxy

Returns:

  • (Array<Symbol>)

    List of methods from the Redis class that we don't want to proxy

[] []= _eval _scan method_missing call dup inspect to_s].freeze

redis-rb polyfills collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool_or_conn) ⇒ ConnectionProxy

Returns a new instance of ConnectionProxy

Parameters:

  • pool_or_conn (Redis, ConnectionPool<Redis>)

    a redis connection, or a pool of redis connections

Raises:

  • (ArgumentError)

    raises an exception if the argument is not one of the required classes



18
19
20
21
22
23
24
25
26
27
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Fallback when calling methods we may not have dynamically created above

Parameters:

  • method (String, Symbol)

    the called method name

  • args (Array<Object>)

    the arguments it was called with

  • block (Proc)

    optionally, the block it was called with

Returns:

  • (Object)

    whatever the method returns



100
101
102
103
104
105
106
107
108
# 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

Instance Method Details

#with {|Redis| ... } ⇒ Object

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

Yields:

  • (Redis)

    a direct redis connection

Returns:

  • (Object)

    whatever the passed block evaluates to, nil otherwise



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object

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



76
77
78
79
80
81
82
# 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