Class: Workarea::Configuration::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/workarea/configuration/redis.rb

Constant Summary collapse

DEFAULT =
{ host: 'localhost', port: 6379, db: 0, scheme: 'redis' }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Redis

Returns a new instance of Redis.



58
59
60
# File 'lib/workarea/configuration/redis.rb', line 58

def initialize(config)
  @config = config.to_h.deep_symbolize_keys
end

Instance Attribute Details

#configObject (readonly) Also known as: to_h

Returns the value of attribute config.



55
56
57
# File 'lib/workarea/configuration/redis.rb', line 55

def config
  @config
end

Class Method Details

.cacheObject

Use this for Rails.cache and Rack::Cache config or other ephemeral Redis needs.

Falls back to the persistent config if there is no config for Redis cache.

Note that this isn’t applied automatically - application config can/should use Workarea::Configuration::Redis.cache if caching is being set up with Redis.



22
23
24
25
26
27
28
# File 'lib/workarea/configuration/redis.rb', line 22

def cache
  @cache ||=
    begin
      config = find_config(:redis_cache)
      config == DEFAULT ? persistent : new(config)
    end
end

.find_config(name) ⇒ Object

Looks in order at Workarea.config, Rails secrets, ENV. Recommended to config both WORKAREA_REDIS_* keys and WORKAREA_REDIS_CACHE_* keys with separate DBs or separate servers.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/workarea/configuration/redis.rb', line 34

def find_config(name)
  config_slug = name.to_s.underscore.downcase
  from_config = Workarea.config[config_slug].presence ||
                  Rails.application.secrets[config_slug]

  return from_config if from_config.present?

  env_slug = name.to_s.underscore.upcase
  scheme = ENV["WORKAREA_#{env_slug}_SCHEME"].presence || DEFAULT[:scheme]

  {
    scheme: scheme,
    host: ENV["WORKAREA_#{env_slug}_HOST"].presence || DEFAULT[:host],
    port: ENV["WORKAREA_#{env_slug}_PORT"].presence || DEFAULT[:port],
    db: ENV["WORKAREA_#{env_slug}_DB"].presence || DEFAULT[:db],
    password: ENV["WORKAREA_#{env_slug}_PASSWORD"].presence,
    ssl: scheme == 'rediss' ? true : false
  }
end

.persistentObject

Used for Sidekiq and Predictor



8
9
10
# File 'lib/workarea/configuration/redis.rb', line 8

def persistent
  @persistent ||= new(find_config(:redis))
end

Instance Method Details

#dbObject



82
83
84
# File 'lib/workarea/configuration/redis.rb', line 82

def db
  @config[:db]
end

#hostObject



70
71
72
# File 'lib/workarea/configuration/redis.rb', line 70

def host
  @config[:host]
end

#passwordObject



74
75
76
# File 'lib/workarea/configuration/redis.rb', line 74

def password
  @config[:password]
end

#portObject



78
79
80
# File 'lib/workarea/configuration/redis.rb', line 78

def port
  @config[:port]
end

#schemeObject



62
63
64
# File 'lib/workarea/configuration/redis.rb', line 62

def scheme
  @config[:scheme]
end

#sslObject



66
67
68
# File 'lib/workarea/configuration/redis.rb', line 66

def ssl
  @config[:ssl]
end

#to_urlObject



86
87
88
89
90
91
92
93
# File 'lib/workarea/configuration/redis.rb', line 86

def to_url
  base = "#{scheme}://"
  base << "admin:#{password}@" if password.present?
  base << "#{host}"
  base << ":#{port}" if port.present?
  base << "/#{db}" if db.present?
  base
end