Class: Hpreserve::FileCacher
- Inherits:
-
AbstractCacher
- Object
- AbstractCacher
- Hpreserve::FileCacher
- Defined in:
- lib/hpreserve/file_cacher.rb
Instance Attribute Summary collapse
-
#base_dir ⇒ Object
Returns the value of attribute base_dir.
Attributes inherited from AbstractCacher
Instance Method Summary collapse
- #expire(keys) ⇒ Object
-
#initialize(patterns, base) ⇒ FileCacher
constructor
A new instance of FileCacher.
- #retrieve(key) ⇒ Object
- #store(key, value) ⇒ Object
Methods inherited from AbstractCacher
Constructor Details
#initialize(patterns, base) ⇒ FileCacher
Returns a new instance of FileCacher.
5 6 7 8 |
# File 'lib/hpreserve/file_cacher.rb', line 5 def initialize(patterns, base) super(patterns) self.base_dir = base end |
Instance Attribute Details
#base_dir ⇒ Object
Returns the value of attribute base_dir.
3 4 5 |
# File 'lib/hpreserve/file_cacher.rb', line 3 def base_dir @base_dir end |
Instance Method Details
#expire(keys) ⇒ Object
21 22 23 |
# File 'lib/hpreserve/file_cacher.rb', line 21 def expire(keys) Dir[File.join(base_dir, keys)].each {|f| File.delete(f) } end |
#retrieve(key) ⇒ Object
16 17 18 19 |
# File 'lib/hpreserve/file_cacher.rb', line 16 def retrieve(key) file = File.join(base_dir, key) File.read(file) if File.exists?(file) end |
#store(key, value) ⇒ Object
10 11 12 13 14 |
# File 'lib/hpreserve/file_cacher.rb', line 10 def store(key, value) file = File.join(base_dir, key) FileUtils.mkdir_p(File.dirname(file)) unless File.directory?(File.dirname(file)) File.open(file, 'w') {|f| f << value } end |