Class: Jsus::Util::FileCache
- Inherits:
-
Object
- Object
- Jsus::Util::FileCache
- Defined in:
- lib/jsus/util/file_cache.rb
Overview
Simple file cache manager.
Instance Method Summary (collapse)
-
- (Object) delete(key)
Deletes cache entry for given key.
-
- (String) fetch(key) { ... }
Path to stored file.
-
- (FileCache) initialize(path)
constructor
Initializes filecache to given directory.
-
- (String?) read(key)
(also: #exists?)
Path to cached file or nil.
-
- (String) write(key, value)
Creates a file with given value for given key in cache directory.
Constructor Details
- (FileCache) initialize(path)
Initializes filecache to given directory
11 12 13 |
# File 'lib/jsus/util/file_cache.rb', line 11 def initialize(path) @path = path end |
Instance Method Details
- (Object) delete(key)
Deletes cache entry for given key.
48 49 50 51 52 53 |
# File 'lib/jsus/util/file_cache.rb', line 48 def delete(key) item_path = generate_path(key) if File.exists?(item_path) FileUtils.rm_f(item_path) end end |
- (String) fetch(key) { ... }
Path to stored file
41 42 43 |
# File 'lib/jsus/util/file_cache.rb', line 41 def fetch(key, &block) read(key) || write(key, yield) end |
- (String?) read(key) Also known as: exists?
Path to cached file or nil
31 32 33 34 |
# File 'lib/jsus/util/file_cache.rb', line 31 def read(key) item_path = generate_path(key) File.exists?(item_path) ? item_path : nil end |
- (String) write(key, value)
Creates a file with given value for given key in cache directory
21 22 23 24 25 26 |
# File 'lib/jsus/util/file_cache.rb', line 21 def write(key, value) item_path = generate_path(key) FileUtils.mkdir_p(File.dirname(item_path)) File.open(item_path, 'w+') {|f| f.write(value) } item_path end |