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 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Redis

Returns a new instance of Redis.



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

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.



51
52
53
# File 'lib/workarea/configuration/redis.rb', line 51

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
# 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

  {
    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]
  }
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



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

def db
  @config[:db]
end

#hostObject



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

def host
  @config[:host]
end

#portObject



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

def port
  @config[:port]
end

#to_urlObject



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

def to_url
  base = "redis://#{host}"
  base << ":#{port}" if port.present?
  base << "/#{db}" if db.present?
  base
end