2022-04-28 08:47:34 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RedisConfiguration
|
|
|
|
class << self
|
2022-04-29 13:43:07 -07:00
|
|
|
def establish_pool(new_pool_size)
|
|
|
|
@pool&.shutdown(&:close)
|
|
|
|
@pool = ConnectionPool.new(size: new_pool_size) { new.connection }
|
|
|
|
end
|
|
|
|
|
2022-09-26 18:08:19 -07:00
|
|
|
delegate :with, to: :pool
|
2022-04-28 08:47:34 -07:00
|
|
|
|
|
|
|
def pool
|
2022-04-29 13:43:07 -07:00
|
|
|
@pool ||= establish_pool(pool_size)
|
2022-04-28 08:47:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def pool_size
|
|
|
|
if Sidekiq.server?
|
2022-09-26 18:08:19 -07:00
|
|
|
Sidekiq[:concurrency]
|
2022-04-28 08:47:34 -07:00
|
|
|
else
|
|
|
|
ENV['MAX_THREADS'] || 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def connection
|
|
|
|
if namespace?
|
|
|
|
Redis::Namespace.new(namespace, redis: raw_connection)
|
|
|
|
else
|
|
|
|
raw_connection
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace?
|
|
|
|
namespace.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace
|
|
|
|
ENV.fetch('REDIS_NAMESPACE', nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
ENV['REDIS_URL']
|
|
|
|
end
|
|
|
|
|
2024-06-17 05:28:01 -07:00
|
|
|
def redis_driver
|
|
|
|
ENV.fetch('REDIS_DRIVER', 'hiredis') == 'ruby' ? :ruby : :hiredis
|
|
|
|
end
|
|
|
|
|
2022-04-28 08:47:34 -07:00
|
|
|
private
|
|
|
|
|
|
|
|
def raw_connection
|
2024-06-17 05:28:01 -07:00
|
|
|
Redis.new(url: url, driver: redis_driver)
|
2022-04-28 08:47:34 -07:00
|
|
|
end
|
|
|
|
end
|