Class: FileCache

Inherits:
Object show all
Defined in:
lib/file_cache.rb

Constant Summary collapse

MAXDURATION =
60 * 10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ FileCache



23
24
25
# File 'lib/file_cache.rb', line 23

def initialize(filepath)
  @filepath = filepath
end

Instance Attribute Details

#filepathObject (readonly)

Returns the value of attribute filepath.



22
23
24
# File 'lib/file_cache.rb', line 22

def filepath
  @filepath
end

Class Method Details

.clear(filepath) ⇒ Object



12
13
14
# File 'lib/file_cache.rb', line 12

def self.clear(filepath)
  new(filepath).clear!
end

.clear_allObject



16
17
18
19
20
# File 'lib/file_cache.rb', line 16

def self.clear_all
  File.join(Dir.tmpdir, "dcm-*.dump")
    .then { Dir.glob(_1) }
    .each { FileUtils.rm(_1) }
end

.get(filepath, &blk) ⇒ Object



8
9
10
# File 'lib/file_cache.rb', line 8

def self.get(filepath, &blk)
  new(filepath).cache!(&blk)
end

Instance Method Details

#cache!Object



27
28
29
30
31
# File 'lib/file_cache.rb', line 27

def cache!
  return read_cache if cached?

  yield.tap { write_cache(_1) }
end

#clear!Object



33
34
35
# File 'lib/file_cache.rb', line 33

def clear!
  FileUtils.rm(cachefilepath) if exist?
end