Module: Bearcat::RedisConnection

Defined in:
lib/bearcat/redis_connection.rb

Constant Summary collapse

KNOWN_REDIS_URL_ENVS =
%w[REDIS_URL REDISTOGO_URL REDISCLOUD_URL].freeze

Class Method Summary collapse

Class Method Details

.configured?(prefix, explicit: true) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/bearcat/redis_connection.rb', line 14

def configured?(prefix, explicit: true)
  determine_redis_provider(prefix: prefix, explicit: explicit).present?
end

.create(options = {}) ⇒ Object



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
47
48
49
50
51
52
53
54
# File 'lib/bearcat/redis_connection.rb', line 18

def create(options = {})
  symbolized_options = options.transform_keys(&:to_sym)

  if !symbolized_options[:url] && (u = determine_redis_provider(prefix: options[:env_prefix]))
    symbolized_options[:url] = u
  end

  size = if symbolized_options[:size]
    symbolized_options[:size]
  elsif symbolized_options[:env_prefix] && (v = ENV["#{symbolized_options[:env_prefix]}_REDIS_POOL_SIZE"])
    Integer(v)
  elsif ENV["RAILS_MAX_THREADS"]
    Integer(ENV["RAILS_MAX_THREADS"])
  else
    5
  end

  pool_timeout = symbolized_options[:pool_timeout] || 1

  ConnectionPool.new(timeout: pool_timeout, size: size) do
    namespace = symbolized_options[:namespace]
    client = Redis.new client_opts(symbolized_options)

    if namespace
      begin
        require "redis/namespace"
        Redis::Namespace.new(namespace, redis: client)
      rescue LoadError
        Rails.logger.error("Your Redis configuration uses the namespace '#{namespace}' but the redis-namespace gem is not included in the Gemfile." \
                             "Add the gem to your Gemfile to continue using a namespace. Otherwise, remove the namespace parameter.")
        exit(-127)
      end
    else
      client
    end
  end
end