class Piggly::Compiler::CacheDir

Each cache unit (any group of data that should be expired and created together) can be broken apart, to prevent unmarshaling a huge block of data all at once.

The interface works like a Hash, so the compile method should return a hash of objects. Each object is writen to a different file (named by the hash key) within the same directory. String objects are (usually) read and written directly to disk, while all other objects are (un-)Marshal'd

Cache invalidation is done by comparing mtime timestamps on the cached object's file to all the “source” files (ruby libs, input files, etc) required to regenerate the data.

Constants

HINT

Non-printable ASCII char indicates data should be Marshal'd

Public Class Methods

new(dir) click to toggle source
# File lib/piggly/compiler/cache_dir.rb, line 24
def initialize(dir)
  @dir  = dir
  @data = Hash.new do |h, k|
    path = File.join(@dir, k.to_s)
    if File.exists?(path)
      h[k.to_s] = File.open(path, "rb") do |io|
        # Detect Marshal'd data
        if io.read(2) !~ HINT
          io.rewind
          io.read
        else
          io.rewind
          Marshal.load(io)
        end
      end
    end
  end
end

Public Instance Methods

[](key) click to toggle source

Load given key from file system into memory if needed

@return [Object]
# File lib/piggly/compiler/cache_dir.rb, line 45
def [](key)
  @data[key.to_s]
end
[]=(key, value) click to toggle source

Writes through to file system

@return [void]
# File lib/piggly/compiler/cache_dir.rb, line 51
def []=(key, value)
  @data[key.to_s] = value
  write(key.to_s => value)
end
clear() click to toggle source

Creates cachedir, destroys its contents, and returns self

@return [CacheDir] self
# File lib/piggly/compiler/cache_dir.rb, line 77
def clear
  @data.clear

  if File.exists?(@dir)
    FileUtils.rm(Dir["#{@dir}/*"])
    FileUtils.touch(@dir)
  else
    FileUtils.mkdir(@dir) 
  end

  self
end
delete(key) click to toggle source

@return [void]

# File lib/piggly/compiler/cache_dir.rb, line 64
def delete(key)
  path = File.join(@dir, key.to_s)
  File.unlink(path) if File.exists?(path)
  @data.delete(key)
end
keys() click to toggle source

@return [Array<String>]

# File lib/piggly/compiler/cache_dir.rb, line 71
def keys
  Dir[@dir + "/*"].map{|f| File.basename(f) }
end
replace(hash) click to toggle source

Clears entire cache, replaces contents, and returns self

@return [CacheDir] self
# File lib/piggly/compiler/cache_dir.rb, line 92
def replace(hash)
  clear
  update(hash)
end
update(hash) click to toggle source

Writes through to file system and returns self

@return [CacheDir] self
# File lib/piggly/compiler/cache_dir.rb, line 58
def update(hash)
  hash.each{|k,v| self[k] = v }
  self
end

Private Instance Methods

write(hash) click to toggle source

Serializes each entry to disk

@return [void]
# File lib/piggly/compiler/cache_dir.rb, line 101
def write(hash)
  FileUtils.mkdir(@dir) unless File.exists?(@dir)
  FileUtils.touch(@dir) # Update mtime

  hash.each do |key, value|
    File.open(File.join(@dir, key.to_s), "wb") do |io|
      # Marshal if the first two bytes contain non-ASCII
      if value.is_a?(String) and value[0,2] !~ HINT
        io.write(value)
      else
        Marshal.dump(value, io)
      end
    end
  end
end