Class: Workarea::Configuration::Redis
- Inherits:
-
Object
- Object
- Workarea::Configuration::Redis
- Defined in:
- lib/workarea/configuration/redis.rb
Constant Summary collapse
- DEFAULT =
{ host: 'localhost', port: 6379, db: 0, scheme: 'redis' }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
-
.cache ⇒ Object
Use this for Rails.cache and Rack::Cache config or other ephemeral Redis needs.
-
.find_config(name) ⇒ Object
Looks in order at Workarea.config, Rails secrets, ENV.
-
.persistent ⇒ Object
Used for Sidekiq and Predictor.
Instance Method Summary collapse
- #db ⇒ Object
- #host ⇒ Object
-
#initialize(config) ⇒ Redis
constructor
A new instance of Redis.
- #password ⇒ Object
- #port ⇒ Object
- #scheme ⇒ Object
- #ssl? ⇒ Boolean (also: #ssl)
- #to_h ⇒ Object
- #to_url ⇒ Object
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
#config ⇒ Object (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
.cache ⇒ Object
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 |
.persistent ⇒ Object
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
#db ⇒ Object
78 79 80 |
# File 'lib/workarea/configuration/redis.rb', line 78 def db @config[:db].presence || DEFAULT[:db] end |
#host ⇒ Object
66 67 68 |
# File 'lib/workarea/configuration/redis.rb', line 66 def host @config[:host].presence || DEFAULT[:host] end |
#password ⇒ Object
70 71 72 |
# File 'lib/workarea/configuration/redis.rb', line 70 def password @config[:password] end |
#port ⇒ Object
74 75 76 |
# File 'lib/workarea/configuration/redis.rb', line 74 def port @config[:port].presence || DEFAULT[:port] end |
#scheme ⇒ Object
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
61 62 63 |
# File 'lib/workarea/configuration/redis.rb', line 61 def ssl? scheme == 'rediss' end |
#to_h ⇒ Object
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_url ⇒ Object
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 |