Class: Cyrel::AST::OptimizedCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cyrel/ast/optimized_nodes.rb

Overview

Optimized cache that takes advantage of Data’s hash/==

Instance Method Summary collapse

Constructor Details

#initializeOptimizedCache

Returns a new instance of OptimizedCache.



42
43
44
45
46
# File 'lib/cyrel/ast/optimized_nodes.rb', line 42

def initialize
  @cache = {}
  @max_size = 1000
  @mutex = Mutex.new
end

Instance Method Details

#fetch(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cyrel/ast/optimized_nodes.rb', line 48

def fetch(node)
  # Data objects have reliable hash/== so we can use them directly as keys
  @mutex.synchronize do
    if @cache.key?(node)
      @cache[node]
    else
      value = yield
      store(node, value)
      value
    end
  end
end