Class: Cache
- Inherits:
-
Object
- Object
- Cache
- Defined in:
- lib/fresnel/cache.rb
Instance Attribute Summary collapse
-
#active ⇒ Object
Returns the value of attribute active.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #clear(options) ⇒ Object
- #create(options, &block) ⇒ Object
-
#initialize(options = Hash.new) ⇒ Cache
constructor
A new instance of Cache.
- #load(options, &block) ⇒ Object
- #log(str) ⇒ Object
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(=Hash.new) @active=[:active]||true end |
Instance Attribute Details
#active ⇒ Object
Returns the value of attribute active.
2 3 4 |
# File 'lib/fresnel/cache.rb', line 2 def active @active end |
#timeout ⇒ Object
Returns the value of attribute timeout.
2 3 4 |
# File 'lib/fresnel/cache.rb', line 2 def timeout @timeout end |
Class Method Details
.clear_all ⇒ Object
8 9 10 |
# File 'lib/fresnel/cache.rb', line 8 def self.clear_all `rm -rf /tmp/fresnel*` end |
Instance Method Details
#clear(options) ⇒ Object
73 74 75 76 |
# File 'lib/fresnel/cache.rb', line 73 def clear() log "clearing cache /tmp/fresnel_#{[:name]}.yml" File.delete("/tmp/fresnel_#{[:name]}.yml") if File.exists?("/tmp/fresnel_#{[:name]}.yml") end |
#create(options, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/fresnel/cache.rb', line 21 def create(, &block) log "eval #{[:action]}" if block data = block.call elsif [:action] data=eval([:action]) else raise ArgumentError, "No block or code to eval for a cache-able value" end log "creating cache file #{[:name]}..." File.open("/tmp/fresnel_#{[: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
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 67 68 69 70 71 |
# File 'lib/fresnel/cache.rb', line 42 def load(, &block) if self.active cache_timeout=[:timeout]||@@cache_timeout log "caching is active !" if File.exists?("/tmp/fresnel_#{[:name]}.yml") created_at=File.mtime("/tmp/fresnel_#{[: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_#{[: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(, &block) end else log "no cache data found, calling create..." self.create(, &block) end else log "cache disabled, fetching life data (and creating cache file for future use...once cache is enabled)" self.create(, &block) end end |
#log(str) ⇒ Object
15 16 17 18 19 |
# File 'lib/fresnel/cache.rb', line 15 def log(str) if @@debug puts str end end |