Class: Cache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = Hash.new) ⇒ Cache

Returns a new instance of Cache.



4
5
6
# File 'lib/fresnel/cache.rb', line 4

def initialize(options=Hash.new)
  @active=options[:active]||true
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



2
3
4
# File 'lib/fresnel/cache.rb', line 2

def active
  @active
end

#timeoutObject

Returns the value of attribute timeout.



2
3
4
# File 'lib/fresnel/cache.rb', line 2

def timeout
  @timeout
end

Instance Method Details

#clear(options) ⇒ Object



68
69
70
71
# File 'lib/fresnel/cache.rb', line 68

def clear(options)
  log "clearing cache /tmp/fresnel_#{options[:name]}.yml"
  File.delete("/tmp/fresnel_#{options[:name]}.yml") if File.exists?("/tmp/fresnel_#{options[:name]}.yml")
end

#create(options, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fresnel/cache.rb', line 16

def create(options, &block)
  log "eval #{options[:action]}"
  if block
    data = block.call
  elsif options[:action]
    data=eval(options[:action])
  else
    raise ArgumentError, "No block or code to eval for a cache-able value"
  end
  log "creating cache file #{options[:name]}..."
  File.open("/tmp/fresnel_#{options[:name]}.yml",'w+'){ |f| f.write(YAML::dump(data)) }
  def data.age=(seconds)
    @age_in_seconds=seconds
  end
  def data.age
    @age_in_seconds
  end
  data.age=0
  return data
end

#load(options, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fresnel/cache.rb', line 37

def load(options, &block)
  if self.active
    cache_timeout=options[:timeout]||@@cache_timeout
    log "caching is active !"
    if File.exists?("/tmp/fresnel_#{options[:name]}.yml")
      created_at=File.mtime("/tmp/fresnel_#{options[:name]}.yml")
      if (Time.now-created_at) < cache_timeout
        log "returning cached info (age : #{(Time.now-created_at).round}, timeout : #{cache_timeout})"
        data=YAML::load_file("/tmp/fresnel_#{options[:name]}.yml")
        def data.age=(seconds)
          @age_in_seconds=seconds
        end
        def data.age
          @age_in_seconds
        end
        data.age=(Time.now-created_at).round
        return data
      else
        log "refreshing data because the cached is older then the timeout (age : #{(Time.now-created_at).round}, timeout : #{cache_timeout})"
        self.create(options, &block)
      end
    else
      log "no cache data found, calling create..."
      self.create(options, &block)
    end
  else
    log "cache disabled, fetching life data (and creating cache file for future use...once cache is enabled)"
    self.create(options, &block)
  end
end

#log(str) ⇒ Object



10
11
12
13
14
# File 'lib/fresnel/cache.rb', line 10

def log(str)
  if @@debug
    puts str
  end
end