You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
869 B

  1. # frozen_string_literal: true
  2. class RedisConfiguration
  3. class << self
  4. def establish_pool(new_pool_size)
  5. @pool&.shutdown(&:close)
  6. @pool = ConnectionPool.new(size: new_pool_size) { new.connection }
  7. end
  8. def with
  9. pool.with { |redis| yield redis }
  10. end
  11. def pool
  12. @pool ||= establish_pool(pool_size)
  13. end
  14. def pool_size
  15. if Sidekiq.server?
  16. Sidekiq.options[:concurrency]
  17. else
  18. ENV['MAX_THREADS'] || 5
  19. end
  20. end
  21. end
  22. def connection
  23. if namespace?
  24. Redis::Namespace.new(namespace, redis: raw_connection)
  25. else
  26. raw_connection
  27. end
  28. end
  29. def namespace?
  30. namespace.present?
  31. end
  32. def namespace
  33. ENV.fetch('REDIS_NAMESPACE', nil)
  34. end
  35. def url
  36. ENV['REDIS_URL']
  37. end
  38. private
  39. def raw_connection
  40. Redis.new(url: url, driver: :hiredis)
  41. end
  42. end