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.



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

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

Instance Attribute Details

#configObject (readonly)

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

  env_slug = name.to_s.underscore.upcase
  from_config.symbolize_keys.reverse_merge(
    scheme: ENV["WORKAREA_#{env_slug}_SCHEME"],
    host: ENV["WORKAREA_#{env_slug}_HOST"],
    port: ENV["WORKAREA_#{env_slug}_PORT"],
    db: ENV["WORKAREA_#{env_slug}_DB"],
    password: ENV["WORKAREA_#{env_slug}_PASSWORD"]
  )
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



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

def db
  @config[:db].presence || DEFAULT[:db]
end

#hostObject



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

def host
  @config[:host].presence || DEFAULT[:host]
end

#passwordObject



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

def password
  @config[:password]
end

#portObject



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

def port
  @config[:port].presence || DEFAULT[:port]
end

#schemeObject



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

def scheme
  @config[:scheme].presence || DEFAULT[:scheme]
end

#ssl?Boolean Also known as: ssl

Returns:

  • (Boolean)


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

def ssl?
  scheme == 'rediss'
end

#to_hObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/workarea/configuration/redis.rb', line 82

def to_h
  {
    scheme: scheme,
    host: host,
    port: port,
    db: db,
    password: password,
    ssl: ssl?
  }
end

#to_urlObject



93
94
95
96
97
98
99
100
# File 'lib/workarea/configuration/redis.rb', line 93

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