Class: Geminabox::DiskCache
- Inherits:
-
Object
- Object
- Geminabox::DiskCache
- Defined in:
- lib/geminabox/disk_cache.rb
Instance Attribute Summary collapse
-
#root_path ⇒ Object
readonly
Returns the value of attribute root_path.
Instance Method Summary collapse
- #cache(key) ⇒ Object
- #flush ⇒ Object
- #flush_key(key) ⇒ Object
-
#initialize(root_path) ⇒ DiskCache
constructor
A new instance of DiskCache.
- #marshal_cache(key) ⇒ Object
Constructor Details
#initialize(root_path) ⇒ DiskCache
Returns a new instance of DiskCache.
6 7 8 9 |
# File 'lib/geminabox/disk_cache.rb', line 6 def initialize(root_path) @root_path = root_path ensure_dir_exists! end |
Instance Attribute Details
#root_path ⇒ Object (readonly)
Returns the value of attribute root_path.
4 5 6 |
# File 'lib/geminabox/disk_cache.rb', line 4 def root_path @root_path end |
Instance Method Details
#cache(key) ⇒ Object
21 22 23 24 |
# File 'lib/geminabox/disk_cache.rb', line 21 def cache(key) key_hash = key_hash(key) read(key_hash) || write(key_hash, yield) end |
#flush ⇒ Object
16 17 18 19 |
# File 'lib/geminabox/disk_cache.rb', line 16 def flush FileUtils.rm_rf(root_path) ensure_dir_exists! end |
#flush_key(key) ⇒ Object
11 12 13 14 |
# File 'lib/geminabox/disk_cache.rb', line 11 def flush_key(key) path = path(key_hash(key)) FileUtils.rm_f(path) end |
#marshal_cache(key) ⇒ Object
26 27 28 29 |
# File 'lib/geminabox/disk_cache.rb', line 26 def marshal_cache(key) key_hash = key_hash(key) marshal_read(key_hash) || marshal_write(key_hash, yield) end |