Class: SiteDiff::Cache
- Inherits:
-
Object
- Object
- SiteDiff::Cache
- Defined in:
- lib/sitediff/cache.rb
Overview
SiteDiff Cache Handler.
Constant Summary collapse
- TIMESTAMP_FILE =
'timestamp'
Instance Attribute Summary collapse
-
#read_tags ⇒ Object
Returns the value of attribute read_tags.
-
#write_tags ⇒ Object
Returns the value of attribute write_tags.
Instance Method Summary collapse
-
#get(tag, path) ⇒ Object
Get data from cache.
-
#get_dir(directory) ⇒ Object
Ensures that a directory exists.
-
#initialize(opts = {}) ⇒ Cache
constructor
Creates a Cache object.
-
#key(tag, path) ⇒ Object
TODO: Document this or remove it if unused.
-
#set(tag, path, result) ⇒ Object
Set data to cache.
-
#tag?(tag) ⇒ Boolean
Is a tag cached? TODO: Rename it to is_cached? as it makes more sense.
Constructor Details
#initialize(opts = {}) ⇒ Cache
Creates a Cache object.
15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 |
Instance Attribute Details
#read_tags ⇒ Object
Returns the value of attribute read_tags.
11 12 13 |
# File 'lib/sitediff/cache.rb', line 11 def @read_tags end |
#write_tags ⇒ Object
Returns the value of attribute write_tags.
11 12 13 |
# File 'lib/sitediff/cache.rb', line 11 def @write_tags end |
Instance Method Details
#get(tag, path) ⇒ Object
Get data from cache.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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) ⇒ Object
Ensures that a directory exists.
105 106 107 108 109 110 |
# File 'lib/sitediff/cache.rb', line 105 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) ⇒ Object
TODO: Document this or remove it if unused.
98 99 100 101 |
# File 'lib/sitediff/cache.rb', line 98 def key(tag, path) # Ensure encoding stays the same! Marshal.dump([tag, path.encode('UTF-8')]) end |
#set(tag, path, result) ⇒ Object
Set data to cache.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sitediff/cache.rb', line 55 def set(tag, path, result) return unless @write_tags.include? tag (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}.temporary" # 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 File.exist?("#{curdir}/index.html") SiteDiff.log "Overwriting file #{tempname}", :warning end File.rename(tempname, "#{curdir}/index.html") # tempname.rename(curdir + 'index.html') end end File.open(filename, 'w') { |file| file.write(Marshal.dump(result)) } end |
#tag?(tag) ⇒ Boolean
Is a tag cached? TODO: Rename it to is_cached? as it makes more sense.
31 32 33 |
# File 'lib/sitediff/cache.rb', line 31 def tag?(tag) File.directory?(File.join(@dir, 'snapshot', tag.to_s)) end |