class Experimental::Source::Cache
Attributes
cache[RW]
last_update[RW]
source[R]
ttl[R]
Public Class Methods
new(source, options = {})
click to toggle source
A cache source provides in memory caching around another source.
If a :ttl
option is passed, experiments will only be cached for that many seconds, otherwise it is cached forever.
# File lib/experimental/source/cache.rb, line 8 def initialize(source, options = {}) @source = source @ttl = options[:ttl] @last_update = nil @cache = {} end
Public Instance Methods
[](name)
click to toggle source
# File lib/experimental/source/cache.rb, line 17 def [](name) refresh if dirty? cache[name.to_s] end
available()
click to toggle source
# File lib/experimental/source/cache.rb, line 22 def available refresh if dirty? cache.values end
Private Instance Methods
dirty?()
click to toggle source
# File lib/experimental/source/cache.rb, line 31 def dirty? return true if last_update.nil? ttl ? (Time.now.to_f - last_update > ttl) : false end
refresh()
click to toggle source
# File lib/experimental/source/cache.rb, line 36 def refresh cache.clear source.available.each do |experiment| cache[experiment.name] = experiment end self.last_update = Time.now.to_f end