Class: Hpreserve::FileCacher

Inherits:
AbstractCacher show all
Defined in:
lib/hpreserve/file_cacher.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractCacher

#patterns, #storage

Instance Method Summary collapse

Methods inherited from AbstractCacher

#match?

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_dirObject

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