Class: MODL::Parser::ObjectCache

Inherits:
Object
  • Object
show all
Defined in:
lib/modl/parser/object_cache.rb

Overview

Store any files for up to 1 hour by default.

Defined Under Namespace

Classes: CacheEntry

Instance Method Summary collapse

Constructor Details

#initializeObjectCache

Set up and empty cache.



26
27
28
# File 'lib/modl/parser/object_cache.rb', line 26

def initialize
  @cache = {}
end

Instance Method Details

#evict(key) ⇒ Object

Evict a cache entry



36
37
38
# File 'lib/modl/parser/object_cache.rb', line 36

def evict(key)
  @cache.delete(key) unless key.nil?
end

#get(key) ⇒ Object

Return the object with the given key if one exists and has not expired.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/modl/parser/object_cache.rb', line 41

def get(key)
  # Return nothing if not in the cache or it has expired.
  return if key.nil?

  entry = @cache[key]
  return unless entry
  return if entry.expired?

  # Otherwise return the cached object.
  entry.object
end

#put(key, object, ttl = nil) ⇒ Object

Cache an object with the given key and optional ttl in seconds (default 1 hour)



31
32
33
# File 'lib/modl/parser/object_cache.rb', line 31

def put(key, object, ttl = nil)
  @cache[key] = CacheEntry.new(object, ttl) unless key.nil? || object.nil?
end