redis_configuration.rb 830 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. delegate :with, to: :pool
  9. def pool
  10. @pool ||= establish_pool(pool_size)
  11. end
  12. def pool_size
  13. if Sidekiq.server?
  14. Sidekiq[:concurrency]
  15. else
  16. ENV['MAX_THREADS'] || 5
  17. end
  18. end
  19. end
  20. def connection
  21. if namespace?
  22. Redis::Namespace.new(namespace, redis: raw_connection)
  23. else
  24. raw_connection
  25. end
  26. end
  27. def namespace?
  28. namespace.present?
  29. end
  30. def namespace
  31. ENV.fetch('REDIS_NAMESPACE', nil)
  32. end
  33. def url
  34. ENV['REDIS_URL']
  35. end
  36. private
  37. def raw_connection
  38. Redis.new(url: url, driver: :hiredis)
  39. end
  40. end