Class: RailsNamedCache::Configuration

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

Overview

Turns the raw config.named_cache_stores value into cache store instances.

Values may be:

  • an ActiveSupport::Cache::Store instance (used as-is)
  • anything ActiveSupport::Cache.lookup_store understands, e.g. :memory_store or [:redis_cache_store, { url: ENV["REDIS_URL"] }]

Configuration only resolves and validates; it never stores anything. The Registry owns state.

Examples:

RailsNamedCache::Configuration.new(
  memory: ActiveSupport::Cache::MemoryStore.new(size: 1.megabyte),
  redis: [:redis_cache_store, { url: ENV.fetch("REDIS_URL") }]
).resolved_stores
# => { memory: #<MemoryStore>, redis: #<RedisCacheStore> }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stores = {}) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • stores (Hash{Symbol=>Object}, nil) (defaults to: {})

    raw config.named_cache_stores

Raises:



27
28
29
30
31
32
33
34
35
36
# File 'lib/rails_named_cache/configuration.rb', line 27

def initialize(stores = {})
  stores ||= {}

  unless stores.is_a?(Hash)
    raise ConfigurationError,
          "config.named_cache_stores must be a Hash of name => cache store, got #{stores.class}"
  end

  @stores = stores
end

Instance Attribute Details

#storesHash (readonly)

Returns the raw configured value.

Returns:

  • (Hash)

    the raw configured value



23
24
25
# File 'lib/rails_named_cache/configuration.rb', line 23

def stores
  @stores
end

Instance Method Details

#resolved_storesHash{Symbol=>ActiveSupport::Cache::Store}

Returns name => store instance.

Returns:

  • (Hash{Symbol=>ActiveSupport::Cache::Store})

    name => store instance

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rails_named_cache/configuration.rb', line 40

def resolved_stores
  stores.each_with_object({}) do |(name, value), resolved|
    key = RailsNamedCache.normalize_name(name)

    # "memory" and :memory are the same store; silently keeping the last one
    # would hide a typo in config.
    if resolved.key?(key)
      raise ConfigurationError,
            "Duplicate named cache store #{key.inspect} in config.named_cache_stores"
    end

    resolved[key] = resolve(key, value)
  end
end