Class: Primer::Cache

Inherits:
Object
  • Object
show all
Includes:
Faye::Timeouts, Watcher
Defined in:
lib/primer/cache.rb,
lib/primer/cache/redis.rb,
lib/primer/cache/memory.rb

Direct Known Subclasses

Memory, Redis

Defined Under Namespace

Classes: Memory, Redis

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Watcher

call_log, included, log, loggers, on_disable, on_enable, register, reset!, watching

Methods included from Enabler

#disable!, #enable!, #enabled?

Instance Attribute Details

#routes(&block) ⇒ Object



19
20
21
22
23
# File 'lib/primer/cache.rb', line 19

def routes(&block)
  @routes ||= RouteSet.new
  @routes.instance_eval(&block) if block_given?
  @routes
end

#throttleObject

Returns the value of attribute throttle.



12
13
14
# File 'lib/primer/cache.rb', line 12

def throttle
  @throttle
end

Instance Method Details

#bind_to_busObject



25
26
27
# File 'lib/primer/cache.rb', line 25

def bind_to_bus
  Primer.bus.subscribe(:changes) { |attribute| changed(attribute) }
end

#changed(attribute) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/primer/cache.rb', line 54

def changed(attribute)
  keys_for_attribute(attribute).each do |cache_key|
    block = lambda do
      invalidate(cache_key)
      regenerate(cache_key)
    end
    @throttle ? timeout(cache_key, &block) : block.call
  end
end

#compute(cache_key) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/primer/cache.rb', line 29

def compute(cache_key)
  return get(cache_key) if has_key?(cache_key)
  
  unless block_given? or @routes
    message = "Cannot call Cache#compute(#{cache_key}) with no block: no routes have been configured"
    raise RouteNotFound.new(message)
  end
  
  calls = []
  result = Watcher.watching(calls) do
    block_given? ? yield : @routes.evaluate(cache_key)
  end
  
  attributes = calls.map do |(receiver, method_name, args, block, return_value)|
    receiver.primer_identifier + [method_name.to_s] + args
  end
  
  unless result.nil?
    relate(cache_key, attributes)
    put(cache_key, result)
  end
  
  result
end

#primer_identifierObject



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

def primer_identifier
  [Cache.name]
end