Class: Redis::Rack::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/redis/rack/connection.rb', line 4

def initialize(options = {})
  @options = options
  @store = options[:redis_store]
  @pool = options[:pool]

  if @pool && !@pool.is_a?(ConnectionPool)
    raise ArgumentError, "pool must be an instance of ConnectionPool"
  end

  if @store && !@store.is_a?(Redis::Store)
    raise ArgumentError, "redis_store must be an instance of Redis::Store (currently #{@store.class.name})"
  end
end

Instance Method Details

#poolObject



30
31
32
# File 'lib/redis/rack/connection.rb', line 30

def pool
  @pool ||= ConnectionPool.new(pool_options) { store } if pooled?
end

#pool_optionsObject



38
39
40
41
42
43
# File 'lib/redis/rack/connection.rb', line 38

def pool_options
  {
    size: @options[:pool_size],
    timeout: @options[:pool_timeout]
  }.reject { |key, value| value.nil? }.to_h
end

#pooled?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/redis/rack/connection.rb', line 26

def pooled?
  [:pool, :pool_size, :pool_timeout].any? { |key| @options.key?(key) }
end

#storeObject



34
35
36
# File 'lib/redis/rack/connection.rb', line 34

def store
  @store ||= Redis::Store::Factory.create(@options[:redis_server])
end

#with(&block) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/redis/rack/connection.rb', line 18

def with(&block)
  if pooled?
    pool.with(&block)
  else
    block.call(store)
  end
end