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, 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

#compute(cache_key) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/primer/cache.rb', line 25

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

#regenerate(cache_key) ⇒ Object



50
51
52
# File 'lib/primer/cache.rb', line 50

def regenerate(cache_key)
  compute(cache_key) rescue nil
end