Module: ContainerConfig::Redis

Defined in:
lib/container_config/redis.rb

Overview

Redis configuration module

Class Method Summary collapse

Class Method Details

.load(key, **options) ⇒ Hash

Loads Redis configuration settings from environment variables, mounted secrets, or the application credentials

Parameters:

  • key (String)

    base key for the Redis config (“QUEUE”, “CACHE”, etc.)

  • options (Hash)

    Options Hash

Options Hash (**options):

  • :required (Boolean)

    whether to raise an exception if the settings cannot be found

  • :secret_mount_directory (String)

    directory where secret files are mounted

Returns:

  • (Hash)

    Redis configuration hash with :url, :password, and :sentinels keys See github.com/redis/redis-rb for more information



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/container_config/redis.rb', line 19

def self.load(key, **options)
  host = ContainerConfig.load("#{key}_HOST", **options, default: "localhost")
  port = ContainerConfig.load("#{key}_PORT", **options, default: "6379")

  url = ContainerConfig.load("#{key}_URL", **options, default: "redis://#{host}:#{port}")
  sentinels = sentinel_info(key, **options)
  password = ContainerConfig.load("#{key}_PASSWORD", **options)
  db = ContainerConfig.load("#{key}_DB", **options, type: :integer, coerce_nil: false)

  # Ensure we never pass an empty string to Redis since it will be passed to the Redis AUTH
  # command as-is and will cause an exception
  password = nil if password.to_s.strip.empty?

  redis_config = { url: url, password: password }
  redis_config[:db] = db if db
  redis_config[:sentinels] = sentinels unless sentinels.empty?

  # Add SSL configuration
  redis_config[:ssl] = ContainerConfig.load("#{key}_SSL", **options, default: false)
  redis_config[:ssl_params] = ssl_params(key, **options)

  redis_config
end