Class: PuppetLibrary::Http::Cache::InMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_library/http/cache/in_memory.rb

Defined Under Namespace

Classes: Entry, Reaper

Constant Summary collapse

ARBITRARY_CACHE_TTL_MILLIS =
10 * 1000

Instance Method Summary collapse

Constructor Details

#initialize(millis_to_live = ARBITRARY_CACHE_TTL_MILLIS) ⇒ InMemory

Returns a new instance of InMemory.



21
22
23
# File 'lib/puppet_library/http/cache/in_memory.rb', line 21

def initialize(millis_to_live = ARBITRARY_CACHE_TTL_MILLIS)
    @reaper = Reaper.new(millis_to_live)
end

Instance Method Details

#get(key) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet_library/http/cache/in_memory.rb', line 25

def get(key)
    entry = retrieve(key)
    if entry
        return entry.value unless @reaper.wants_to_kill? entry
    end

    value = yield
    save(key, Entry.new(value))
    return value
end

#retrieve(key) ⇒ Object



36
37
38
# File 'lib/puppet_library/http/cache/in_memory.rb', line 36

def retrieve(key)
    cache[key]
end

#save(key, entry) ⇒ Object



40
41
42
# File 'lib/puppet_library/http/cache/in_memory.rb', line 40

def save(key, entry)
    cache[key] = entry
end