Class: ContainerEnv::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/container_env/fetcher.rb

Overview

:nodoc:

Constant Summary collapse

SENTINEL =
Object.new.freeze

Instance Method Summary collapse

Constructor Details

#initialize(cache:, config:, env: ENV) ⇒ Fetcher

Returns a new instance of Fetcher.



8
9
10
11
12
# File 'lib/container_env/fetcher.rb', line 8

def initialize(cache:, config:, env: ENV)
  @env = env
  @cache = cache
  @config = config
end

Instance Method Details

#[](key) ⇒ Object



29
30
31
# File 'lib/container_env/fetcher.rb', line 29

def [](key)
  fetch(key)
end

#fetch(key, default = SENTINEL) ⇒ Object

Reads cache_enabled? once to avoid a double call (#8). Delegates to Cache#fetch_or_store when caching is on, which narrows the stampede window compared to separate get/lookup/set calls (#3).



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/container_env/fetcher.rb', line 17

def fetch(key, default = SENTINEL, &)
  key = key.to_s
  value = if @config.cache_enabled?
            @cache.fetch_or_store(key) { lookup(key) }
          else
            lookup(key)
          end
  return value unless value.nil?

  resolve_missing(key, default, &)
end