Class: Piggly::Compiler::CacheDir
- Defined in:
- lib/piggly/compiler/cache_dir.rb
Overview
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.
Constant Summary collapse
- HINT =
Non-printable ASCII char indicates data should be Marshal’d
/[\000-\010\016-\037\177-\300]/
Instance Method Summary collapse
-
#[](key) ⇒ Object
Load given key from file system into memory if needed @return [Object].
-
#[]=(key, value) ⇒ Object
Writes through to file system @return [void].
-
#clear ⇒ Object
Creates cachedir, destroys its contents, and returns self @return [CacheDir] self.
- #delete(key) ⇒ void
-
#initialize(dir) ⇒ CacheDir
constructor
A new instance of CacheDir.
- #keys ⇒ Array<String>
-
#replace(hash) ⇒ Object
Clears entire cache, replaces contents, and returns self @return [CacheDir] self.
-
#update(hash) ⇒ Object
Writes through to file system and returns self @return [CacheDir] self.
Constructor Details
#initialize(dir) ⇒ CacheDir
Returns a new instance of CacheDir.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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 |
Instance Method Details
#[](key) ⇒ Object
Load given key from file system into memory if needed
@return [Object]
45 46 47 |
# File 'lib/piggly/compiler/cache_dir.rb', line 45 def [](key) @data[key.to_s] end |
#[]=(key, value) ⇒ Object
Writes through to file system
@return [void]
51 52 53 54 |
# File 'lib/piggly/compiler/cache_dir.rb', line 51 def []=(key, value) @data[key.to_s] = value write(key.to_s => value) end |
#clear ⇒ Object
Creates cachedir, destroys its contents, and returns self
@return [CacheDir] self
77 78 79 80 81 82 83 84 85 86 87 88 |
# 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) ⇒ void
This method returns an undefined value.
64 65 66 67 68 |
# 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 ⇒ Array<String>
71 72 73 |
# File 'lib/piggly/compiler/cache_dir.rb', line 71 def keys Dir[@dir + "/*"].map{|f| File.basename(f) } end |
#replace(hash) ⇒ Object
Clears entire cache, replaces contents, and returns self
@return [CacheDir] self
92 93 94 95 |
# File 'lib/piggly/compiler/cache_dir.rb', line 92 def replace(hash) clear update(hash) end |
#update(hash) ⇒ Object
Writes through to file system and returns self
@return [CacheDir] self
58 59 60 61 |
# File 'lib/piggly/compiler/cache_dir.rb', line 58 def update(hash) hash.each{|k,v| self[k] = v } self end |