class Glim::Cache

Constants

CACHE_PATH

Public Class Methods

getset(path, group = :default) { || ... } click to toggle source
# File lib/cache.rb, line 35
def getset(path, group = :default)
  begin
    mtime = File.stat(path).mtime
    if record = cache.dig(group, path)
      if mtime == record['modified']
        return record['data']
      end
    end

    record = {
      'modified' => mtime,
      'data'     => yield,
    }

    (cache[group] ||= {})[path] = record
    (@updates[group] ||= {})[path] = record if @updates

    record['data']
  rescue Errno::ENOENT
    $log.warn("File does not exist: #{path}")
    nil
  end
end
load() click to toggle source
# File lib/cache.rb, line 8
def load
  cache
end
merge!(updates) click to toggle source
# File lib/cache.rb, line 29
def merge!(updates)
  updates.each do |group, paths|
    (cache[group] ||= {}).merge!(paths)
  end
end
save() click to toggle source
# File lib/cache.rb, line 12
def save
  unless @cache.nil?
    FileUtils.mkdir_p(File.dirname(CACHE_PATH))
    open(CACHE_PATH, 'w') do |io|
      Marshal.dump(cache, io)
    end
  end
end
track_updates=(flag) click to toggle source
# File lib/cache.rb, line 21
def track_updates=(flag)
  @updates = flag ? {} : nil
end
updates() click to toggle source
# File lib/cache.rb, line 25
def updates
  @updates
end

Private Class Methods

cache() click to toggle source
# File lib/cache.rb, line 61
def cache
  @cache ||= open(CACHE_PATH) { |io| Marshal.load(io) } rescue {}
end