File: connection.rb

package info (click to toggle)
ruby-redis-rack 2.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 176 kB
  • sloc: ruby: 558; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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