Class: RGen::Util::FileCacheMap
- Inherits:
-
Object
- Object
- RGen::Util::FileCacheMap
- Defined in:
- lib/rgen/util/file_cache_map.rb
Overview
Implements a cache for storing and loading data associated with arbitrary files. The data is stored in cache files within a subfolder of the folder where the associated files exists. The cache files are protected with a checksum and loaded data will be invalid in case either the associated file are the cache file has changed.
Instance Attribute Summary collapse
-
#version_info ⇒ Object
optional program version info to be associated with the cache files if the program version changes, cached data will also be invalid.
Instance Method Summary collapse
-
#clean_unused(root_path, key_paths) ⇒ Object
remove cache files which are not associated with any file in
key_paths
will only remove files withinroot_path
. -
#initialize(cache_dir, postfix) ⇒ FileCacheMap
constructor
cache_dir
is the name of the subfolder where cache files are createdpostfix
is an extension appended to the original file name for creating the name of the cache file. -
#load_data(key_path, options = {}) ⇒ Object
load data associated with file
key_path
returns :invalid in case either the associated file or the cache file has changed. -
#store_data(key_path, value_data) ⇒ Object
store data
value_data
associated with filekey_path
.
Constructor Details
#initialize(cache_dir, postfix) ⇒ FileCacheMap
cache_dir
is the name of the subfolder where cache files are created postfix
is an extension appended to the original file name for creating the name of the cache file
22 23 24 25 |
# File 'lib/rgen/util/file_cache_map.rb', line 22 def initialize(cache_dir, postfix) @postfix = postfix @cache_dir = cache_dir end |
Instance Attribute Details
#version_info ⇒ Object
optional program version info to be associated with the cache files if the program version changes, cached data will also be invalid
17 18 19 |
# File 'lib/rgen/util/file_cache_map.rb', line 17 def version_info @version_info end |
Instance Method Details
#clean_unused(root_path, key_paths) ⇒ Object
remove cache files which are not associated with any file in key_paths
will only remove files within root_path
80 81 82 83 84 85 86 87 |
# File 'lib/rgen/util/file_cache_map.rb', line 80 def clean_unused(root_path, key_paths) raise "key paths must be within root path" unless key_paths.all?{|p| p.index(root_path) == 0} used_files = key_paths.collect{|p| cache_file(p)} files = Dir[root_path+"/**/"+@cache_dir+"/*"+@postfix] files.each do |f| FileUtils.rm(f) unless used_files.include?(f) end end |
#load_data(key_path, options = {}) ⇒ Object
load data associated with file key_path
returns :invalid in case either the associated file or the cache file has changed
options:
:invalidation_reasons:
an array which will receive symbols indicating why the cache is invalid:
:no_cachefile, :cachefile_corrupted, :keyfile_changed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rgen/util/file_cache_map.rb', line 35 def load_data(key_path, ={}) reasons = [:invalidation_reasons] || [] cf = cache_file(key_path) result = nil begin File.open(cf, "rb") do |f| header = f.read(41) if !header reasons << :cachefile_corrupted return :invalid end checksum = header[0..39] data = f.read if calc_sha1(data) == checksum if calc_sha1_keydata(key_path) == data[0..39] result = data[41..-1] else reasons << :keyfile_changed result = :invalid end else reasons << :cachefile_corrupted result = :invalid end end rescue Errno::ENOENT reasons << :no_cachefile result = :invalid end result end |
#store_data(key_path, value_data) ⇒ Object
store data value_data
associated with file key_path
68 69 70 71 72 73 74 75 76 |
# File 'lib/rgen/util/file_cache_map.rb', line 68 def store_data(key_path, value_data) data = calc_sha1_keydata(key_path) + "\n" + value_data data = calc_sha1(data) + "\n" + data cf = cache_file(key_path) FileUtils.mkdir(File.dirname(cf)) rescue Errno::EEXIST File.open(cf, "wb") do |f| f.write(data) end end |