Class: Experimental::Source::Cache

Inherits:
Base
  • Object
show all
Defined in:
lib/experimental/source/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Cache

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.



8
9
10
11
12
13
# File 'lib/experimental/source/cache.rb', line 8

def initialize(source, options = {})
  @source = source
  @ttl = options[:ttl]
  @last_update = nil
  @cache = {}
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



15
16
17
# File 'lib/experimental/source/cache.rb', line 15

def source
  @source
end

#ttlObject (readonly)

Returns the value of attribute ttl.



15
16
17
# File 'lib/experimental/source/cache.rb', line 15

def ttl
  @ttl
end

Instance Method Details

#[](name) ⇒ Object



17
18
19
20
# File 'lib/experimental/source/cache.rb', line 17

def [](name)
  refresh if dirty?
  cache[name.to_s]
end

#availableObject



22
23
24
25
# File 'lib/experimental/source/cache.rb', line 22

def available
  refresh if dirty?
  cache.values
end