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

Returns a new instance of DiskCache.



8
9
10
11
# File 'lib/geminabox/disk_cache.rb', line 8

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.



6
7
8
# File 'lib/geminabox/disk_cache.rb', line 6

def root_path
  @root_path
end

Instance Method Details

#cache(key) ⇒ Object



23
24
25
26
# File 'lib/geminabox/disk_cache.rb', line 23

def cache(key)
  key_hash = key_hash(key)
  read(key_hash) || write(key_hash, yield)
end

#flushObject



18
19
20
21
# File 'lib/geminabox/disk_cache.rb', line 18

def flush
  FileUtils.rm_rf(root_path)
  ensure_dir_exists!
end

#flush_key(key) ⇒ Object



13
14
15
16
# File 'lib/geminabox/disk_cache.rb', line 13

def flush_key(key)
  path = path(key_hash(key))
  FileUtils.rm_f(path)
end

#marshal_cache(key) ⇒ Object



28
29
30
31
# File 'lib/geminabox/disk_cache.rb', line 28

def marshal_cache(key)
  key_hash = key_hash(key)
  marshal_read(key_hash) || marshal_write(key_hash, yield)
end