class MODL::Parser::ObjectCache

Store any files for up to 1 hour by default.

Public Class Methods

new() click to toggle source

Set up and empty cache.

# File lib/modl/parser/object_cache.rb, line 49
def initialize
  @cache = {}
end

Public Instance Methods

evict(key) click to toggle source

Evict a cache entry

# File lib/modl/parser/object_cache.rb, line 59
def evict(key)
  @cache.delete(key) unless key.nil?
end
force_get(key) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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