Sha256: f2cdf8088b7224d96111d7416832ec73d507867f75fb658bba038d0030b569f7

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

class Redis
  module Rack
    class Connection
      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

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

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

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
redis-rack-2.1.3 lib/redis/rack/connection.rb
redis-rack-2.1.2 lib/redis/rack/connection.rb
redis-rack-2.1.0 lib/redis/rack/connection.rb
redis-rack-2.1.0.pre lib/redis/rack/connection.rb
redis-rack-2.0.6 lib/redis/rack/connection.rb