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.



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

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

Instance Method Details

#get(key) ⇒ Object



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

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



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

def retrieve(key)
    cache[key]
end

#save(key, entry) ⇒ Object



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

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