lib/nanoc/base/memoization.rb in nanoc-4.2.0 vs lib/nanoc/base/memoization.rb in nanoc-4.2.1

- old
+ new

@@ -13,10 +13,12 @@ def initialize(value) @value = value end end + NONE = Object.new + # Memoizes the method with the given name. The modified method will cache # the results of the original method, so that calling a method twice with # the same arguments will short-circuit and return the cached results # immediately. # @@ -55,15 +57,25 @@ define_method(method_name) do |*args| @__memoization_cache ||= {} @__memoization_cache[method_name] ||= {} method_cache = @__memoization_cache[method_name] - if method_cache.key?(args) && method_cache[args].weakref_alive? - method_cache[args].value - else + value = NONE + if method_cache.key?(args) + value = + begin + method_cache[args].value + rescue WeakRef::RefError + NONE + end + end + + if value.equal?(NONE) send(original_method_name, *args).tap do |r| method_cache[args] = WeakRef.new(Wrapper.new(r)) end + else + value end end end end end