Class: RediSesh::ConnectionWrapper
- Inherits:
-
Object
- Object
- RediSesh::ConnectionWrapper
- Defined in:
- lib/redi_sesh/connection_wrapper.rb
Constant Summary collapse
- POOL_KEYS =
i[pool pool_size pool_timeout].freeze
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ConnectionWrapper
constructor
A new instance of ConnectionWrapper.
- #pool ⇒ Object
- #pool_options ⇒ Object
- #pooled? ⇒ Boolean
- #store ⇒ Object
- #with(&block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ConnectionWrapper
Returns a new instance of ConnectionWrapper.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/redi_sesh/connection_wrapper.rb', line 7 def initialize( = {}) = @store = [:redis_store] @pool = [: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
#pool ⇒ Object
33 34 35 |
# File 'lib/redi_sesh/connection_wrapper.rb', line 33 def pool @pool ||= ConnectionPool.new() { store } if pooled? end |
#pool_options ⇒ Object
41 42 43 44 45 46 |
# File 'lib/redi_sesh/connection_wrapper.rb', line 41 def { size: [:pool_size], timeout: [:pool_timeout] }.compact.to_h end |
#pooled? ⇒ 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| .key?(key) } end |
#store ⇒ Object
37 38 39 |
# File 'lib/redi_sesh/connection_wrapper.rb', line 37 def store @store ||= Store.create([: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 |