Class: Geminabox::DiskCache

Inherits:
Object
  • Object
show all
Defined in:
lib/geminabox/disk_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path) ⇒ 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_pathObject (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

#flushObject


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