class SiteDiff::Cache

SiteDiff Cache Handler.

Constants

TIMESTAMP_FILE

Attributes

read_tags[RW]
write_tags[RW]

Public Class Methods

new(opts = {}) click to toggle source

Creates a Cache object.

# File lib/sitediff/cache.rb, line 15
def initialize(opts = {})
  @create = opts[:create]

  # Read and Write tags are sets that can contain :before and :after.
  # They indicate whether we should use the cache for reading or writing.
  @read_tags = Set.new
  @write_tags = Set.new
  @timestamp_flag = { before: false, after: false }

  # The directory used by the cache for storage.
  @dir = opts[:directory] || '.'
end

Public Instance Methods

get(tag, path) click to toggle source

Get data from cache.

# File lib/sitediff/cache.rb, line 37
def get(tag, path)
  return nil unless @read_tags.include? tag

  filename = File.join(
    @dir,
    'snapshot',
    tag.to_s,
    *path.split(File::SEPARATOR)
  )

  filename = File.join(filename, 'index.html') if File.directory?(filename)
  return nil unless File.file? filename

  Marshal.load(File.read(filename))
end
get_dir(directory) click to toggle source

Ensures that a directory exists.

# File lib/sitediff/cache.rb, line 103
def get_dir(directory)
  # Create the dir. Must go before cache initialization!
  @dir = Pathname.new(directory || '.')
  @dir.mkpath unless @dir.directory?
  @dir.to_s
end
key(tag, path) click to toggle source

TODO: Document this or remove it if unused.

# File lib/sitediff/cache.rb, line 96
def key(tag, path)
  # Ensure encoding stays the same!
  Marshal.dump([tag, path.encode('UTF-8')])
end
set(tag, path, result) click to toggle source

Set data to cache.

# File lib/sitediff/cache.rb, line 55
def set(tag, path, result)
  return unless @write_tags.include? tag

  save_timestamp(tag)
  filename = File.join(
    @dir,
    'snapshot',
    tag.to_s,
    *path.split(File::SEPARATOR)
  )

  filename = File.join(filename, 'index.html') if File.directory?(filename)
  filepath = Pathname.new(filename)
  unless filepath.dirname.directory?
    begin
      filepath.dirname.mkpath
    rescue Errno::EEXIST
      curdir = filepath
      curdir = curdir.parent until curdir.exist?
      tempname = curdir.dirname + (curdir.basename.to_s + '.temporary')
      # May cause problems if action is not atomic!
      # Move existing file to dir/index.html first
      # Not robust! Should generate an UUID or something.
      if File.exist?(tempname)
        SiteDiff.log "Overwriting file #{tempname}", :warning
      end
      curdir.rename(tempname)
      filepath.dirname.mkpath
      # Should only happen in strange situations such as when the path
      # is foo/index.html/bar (i.e., index.html is a directory)
      if (curdir + 'index.html').exist?
        SiteDiff.log "Overwriting file #{tempname}", :warning
      end
      tempname.rename(curdir + 'index.html')
    end
  end
  File.open(filename, 'w') { |file| file.write(Marshal.dump(result)) }
end
tag?(tag) click to toggle source

Is a tag cached? TODO: Rename it to is_cached? as it makes more sense.

# File lib/sitediff/cache.rb, line 31
def tag?(tag)
  File.directory?(File.join(@dir, 'snapshot', tag.to_s))
end

Private Instance Methods

save_timestamp(tag) click to toggle source
# File lib/sitediff/cache.rb, line 112
def save_timestamp(tag)
  # run once
  return if @timestamp_flag[tag]

  @timestamp_flag[tag] = true
  cache_dir = File.join(@dir, 'snapshot', tag.to_s)
  if File.exist? cache_dir
    file = File.join(cache_dir, TIMESTAMP_FILE)
    FileUtils.touch(file)
  end
end