Module: Bunch::Cache

Defined in:
lib/bunch/cache.rb

Class Method Summary collapse

Class Method Details

.fetch(fn, &blk) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bunch/cache.rb', line 26

def fetch(fn, &blk)
  current_mtime = File.mtime(fn)

  if stored?(fn) && mtime(fn) == current_mtime
    read(fn)
  else
    content = blk.call
    write(fn, current_mtime, content)
    content
  end
end

.initObject



6
7
8
# File 'lib/bunch/cache.rb', line 6

def init
  @cache = Hash.new { |h, k| h[k] = {} }
end

.mtime(fn) ⇒ Object



18
19
20
# File 'lib/bunch/cache.rb', line 18

def mtime(fn)
  @cache[fn][:mtime]
end

.read(fn) ⇒ Object



14
15
16
# File 'lib/bunch/cache.rb', line 14

def read(fn)
  @cache[fn][:content]
end

.stored?(fn) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/bunch/cache.rb', line 10

def stored?(fn)
  @cache.keys.include?(fn)
end

.write(fn, mtime, content) ⇒ Object



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

def write(fn, mtime, content)
  @cache[fn] = {:mtime => mtime, :content => content}
end