Class: Cache

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

Direct Known Subclasses

DepAnalyzer

Instance Method Summary collapse

Constructor Details

#initialize(cache, timeout = 24) ⇒ Cache

Returns a new instance of Cache.



6
7
8
9
# File 'lib/dep_analyzer.rb', line 6

def initialize(cache, timeout=24)
  @cache = cache
  @timeout = timeout
end

Instance Method Details

#cache(id, timeout = @timeout) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dep_analyzer.rb', line 11

def cache(id, timeout=@timeout)
  Dir.mkdir @cache unless test ?d, @cache
  path = File.join @cache, id

  age = test(?f, path) ? (Time.now - test( ?M, path )) / 3600 : -1

  if age >= 0 and timeout > age then
    warn "from cache" if $DEBUG
    data = File.read(path)
  else
    warn "NOT from cache (#{age} hours old)" if $DEBUG
    data = yield
    File.open(path, "w") do |f|
      f.write data
    end
  end
  return data
end