lib/sitediff/cache.rb in sitediff-1.0.0 vs lib/sitediff/cache.rb in sitediff-1.1.1

- old
+ new

@@ -4,10 +4,12 @@ require 'fileutils' class SiteDiff # SiteDiff Cache Handler. class Cache + TIMESTAMP_FILE = 'timestamp' + attr_accessor :read_tags, :write_tags ## # Creates a Cache object. def initialize(opts = {}) @@ -15,10 +17,11 @@ # 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 @@ -50,10 +53,11 @@ ## # Set data to cache. 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) @@ -99,8 +103,22 @@ 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 + + private + + 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 end end