class Jerakia::Cache::File

Public Class Methods

add(index, data) click to toggle source
# File lib/jerakia/cache/file.rb, line 16
def add(index, data)
  filestate = state(index)
  Jerakia.log.debug("Adding #{index} to file cache with state #{filestate}")
  cache.add(index, data, :state => filestate) if filestate
end
cache() click to toggle source
# File lib/jerakia/cache/file.rb, line 5
def cache
  Jerakia::Cache
end
get(index) click to toggle source
# File lib/jerakia/cache/file.rb, line 59
def get(index)
  cache.get(index)
end
import_file(filename) click to toggle source
# File lib/jerakia/cache/file.rb, line 39
def import_file(filename)
  Jerakia.log.debug("Importing file #{filename} to file cache")
  File.read(filename)
end
retrieve(filename) click to toggle source

If the cache has a valid copy of the file, then we retrieve it, if the cache doesn't have a copy, or if the state has changed, then we should add it to the cache again and overite the existing data.

Returns nil if the file doesn't exist

# File lib/jerakia/cache/file.rb, line 50
def retrieve(filename)
  if valid?(filename)
    Jerakia.log.debug("Using cached contents of #{filename}")
    get(filename)
  else
    add(filename, import_file(filename)) if File.exists?(filename)
  end
end
state(filename) click to toggle source

Returns the latest mtime of the file if the file exists and nil if it doesn't exist.

# File lib/jerakia/cache/file.rb, line 12
def state(filename)
  ::File.stat(filename).mtime if ::File.exist?(filename)
end
valid?(index) click to toggle source
# File lib/jerakia/cache/file.rb, line 22
def valid?(index)
  if cache.in_bucket?(index)
    unless File.exists?(index)
      cache.purge(index)
      return false
    end
    metadata = cache.metadata(index)
    if metadata
      metadata[:state] == state(index)
    else
      false
    end
  else
    false
  end
end