Class: FileCache
Constant Summary collapse
- MAXDURATION =
60 * 10
Instance Attribute Summary collapse
-
#filepath ⇒ Object
readonly
Returns the value of attribute filepath.
Class Method Summary collapse
Instance Method Summary collapse
- #cache! ⇒ Object
- #clear! ⇒ Object
-
#initialize(filepath) ⇒ FileCache
constructor
A new instance of FileCache.
Constructor Details
#initialize(filepath) ⇒ FileCache
Returns a new instance of FileCache.
23 24 25 |
# File 'lib/file_cache.rb', line 23 def initialize(filepath) @filepath = filepath end |
Instance Attribute Details
#filepath ⇒ Object (readonly)
Returns the value of attribute filepath.
22 23 24 |
# File 'lib/file_cache.rb', line 22 def filepath @filepath end |
Class Method Details
.clear(filepath) ⇒ Object
12 13 14 |
# File 'lib/file_cache.rb', line 12 def self.clear(filepath) new(filepath).clear! end |
.clear_all ⇒ Object
16 17 18 19 20 |
# File 'lib/file_cache.rb', line 16 def self.clear_all File.join(Dir.tmpdir, "dcm-*.dump") .then { Dir.glob(_1) } .each { FileUtils.rm(_1, verbose: true) } end |
.get(filepath, &blk) ⇒ Object
8 9 10 |
# File 'lib/file_cache.rb', line 8 def self.get(filepath, &blk) new(filepath).cache!(&blk) end |
Instance Method Details
#cache! ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/file_cache.rb', line 27 def cache! return read_cache if cached? yield.tap { write_cache(_1) } rescue yield end |
#clear! ⇒ Object
35 36 37 |
# File 'lib/file_cache.rb', line 35 def clear! FileUtils.rm(cachefilepath, verbose: true) if exist? end |