Class: RediSesh::ConnectionWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/redi_sesh/connection_wrapper.rb

Constant Summary collapse

POOL_KEYS =
i[pool pool_size pool_timeout].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConnectionWrapper

Returns a new instance of ConnectionWrapper.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/redi_sesh/connection_wrapper.rb', line 7

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

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

  return unless @store && !@store.is_a?(RediSesh::Store)

  raise ArgumentError, "redis_store must be an instance of RediSesh::Store (currently #{@store.class.name})"
end

Instance Method Details

#poolObject



33
34
35
# File 'lib/redi_sesh/connection_wrapper.rb', line 33

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

#pool_optionsObject



41
42
43
44
45
46
# File 'lib/redi_sesh/connection_wrapper.rb', line 41

def pool_options
  {
    size: @options[:pool_size],
    timeout: @options[:pool_timeout]
  }.compact.to_h
end

#pooled?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/redi_sesh/connection_wrapper.rb', line 27

def pooled?
  return @pooled if defined?(@pooled)

  @pooled = POOL_KEYS.any? { |key| @options.key?(key) }
end

#storeObject



37
38
39
# File 'lib/redi_sesh/connection_wrapper.rb', line 37

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

#with(&block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/redi_sesh/connection_wrapper.rb', line 19

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