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.



49
50
51
# File 'lib/modl/parser/object_cache.rb', line 49

def initialize
  @cache = {}
end

Instance Method Details

#evict(key) ⇒ Object

Evict a cache entry



59
60
61
# File 'lib/modl/parser/object_cache.rb', line 59

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

#force_get(key) ⇒ Object

If the file is offline this can be used to retrieve the cached version if we have one.



78
79
80
81
82
83
84
85
# File 'lib/modl/parser/object_cache.rb', line 78

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

  entry = @cache[key]
  return unless entry
  entry.object
end

#get(key) ⇒ Object

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



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/modl/parser/object_cache.rb', line 64

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.
  # We don't delete the cached entry because we might need to force its use if its expired and offline
  entry.object
end

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

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



54
55
56
# File 'lib/modl/parser/object_cache.rb', line 54

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