闭社主体 forked from https://github.com/tootsuite/mastodon
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.

30 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. def setup_redis_env_url(prefix = nil, defaults = true)
  3. prefix = prefix.to_s.upcase + '_' unless prefix.nil?
  4. prefix = '' if prefix.nil?
  5. return if ENV[prefix + 'REDIS_URL'].present?
  6. password = ENV.fetch(prefix + 'REDIS_PASSWORD') { '' if defaults }
  7. host = ENV.fetch(prefix + 'REDIS_HOST') { 'localhost' if defaults }
  8. port = ENV.fetch(prefix + 'REDIS_PORT') { 6379 if defaults }
  9. db = ENV.fetch(prefix + 'REDIS_DB') { 0 if defaults }
  10. ENV[prefix + 'REDIS_URL'] = if [password, host, port, db].all?(&:nil?)
  11. ENV['REDIS_URL']
  12. else
  13. "redis://#{password.blank? ? '' : ":#{password}@"}#{host}:#{port}/#{db}"
  14. end
  15. end
  16. setup_redis_env_url
  17. setup_redis_env_url(:cache, false)
  18. namespace = ENV.fetch('REDIS_NAMESPACE') { nil }
  19. cache_namespace = namespace ? namespace + '_cache' : 'cache'
  20. REDIS_CACHE_PARAMS = {
  21. expires_in: 10.minutes,
  22. namespace: cache_namespace,
  23. }.freeze