Class: Redstruct::ConnectionProxy
- Inherits:
-
Object
- Object
- Redstruct::ConnectionProxy
- Includes:
- Utils::Inspectable
- Defined in:
- lib/redstruct/connection_proxy.rb
Overview
Connection proxy class for the ConnectionPool
Constant Summary collapse
- NON_COMMAND_METHODS =
Returns List of methods from the Redis class that we don’t want to proxy.
%i[[] []= _eval _scan method_missing call dup inspect to_s].freeze
redis-rb polyfills collapse
-
#zlexcount(key, min, max) ⇒ Object
see: redis.io/commands/zlexcount zlexcount is not supported as of redis-rb 3.3.2.
Instance Method Summary collapse
-
#initialize(pool_or_conn) ⇒ ConnectionProxy
constructor
A new instance of ConnectionProxy.
-
#method_missing(method, *args, &block) ⇒ Object
Fallback when calling methods we may not have dynamically created above.
-
#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.
Methods included from Utils::Inspectable
Constructor Details
#initialize(pool_or_conn) ⇒ ConnectionProxy
Returns a new instance of ConnectionProxy.
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
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
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 |