Class: Coradoc::Reference::Resolver::Caching

Inherits:
Resolver::Base
  • Object
show all
Defined in:
lib/coradoc/reference/resolver/caching.rb

Overview

Wraps another resolver with an address-keyed memoization cache. Addresses are value types, so cache keys are stable across calls. Use to avoid re-asking the catalog for the same Edge.address.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inner:) ⇒ Caching

Returns a new instance of Caching.



12
13
14
15
16
# File 'lib/coradoc/reference/resolver/caching.rb', line 12

def initialize(inner:)
  super()
  @inner = inner
  @cache = {}
end

Instance Attribute Details

#innerObject (readonly)

Returns the value of attribute inner.



10
11
12
# File 'lib/coradoc/reference/resolver/caching.rb', line 10

def inner
  @inner
end

Instance Method Details

#clear!Object



27
28
29
# File 'lib/coradoc/reference/resolver/caching.rb', line 27

def clear!
  @cache.clear
end

#resolve(edge) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/coradoc/reference/resolver/caching.rb', line 18

def resolve(edge)
  cached = if @cache.key?(edge.address)
             @cache[edge.address]
           else
             @cache[edge.address] = @inner.resolve(edge)
           end
  cached.for_edge(edge)
end

#sizeObject



31
32
33
# File 'lib/coradoc/reference/resolver/caching.rb', line 31

def size
  @cache.size
end