Class: Redstruct::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/redstruct/connection.rb

Constant Summary collapse

NON_COMMAND_METHODS =

Returns List of methods from the Redis class that we don’t want to delegate to.

Returns:

  • (Array<Symbol>)

    List of methods from the Redis class that we don’t want to delegate to.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ Connection

Returns a new instance of Connection.

Raises:



8
9
10
11
# File 'lib/redstruct/connection.rb', line 8

def initialize(pool)
  raise(Redstruct::Error, 'Requires a ConnectionPool to proxy to') unless pool.is_a?(ConnectionPool)
  @pool = pool
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



6
7
8
# File 'lib/redstruct/connection.rb', line 6

def pool
  @pool
end

Instance Method Details

#withObject

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

Returns:

  • (Object)

    whatever the passed block evaluates to, nil otherwise



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/redstruct/connection.rb', line 17

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

  return result
end