Class: RSpec::Core::MemoizedHelpers::ThreadsafeMemoized

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/core/memoized_helpers.rb

Instance Method Summary collapse

Constructor Details

#initializeThreadsafeMemoized

Returns a new instance of ThreadsafeMemoized



149
150
151
152
# File 'lib/rspec/core/memoized_helpers.rb', line 149

def initialize
  @memoized = {}
  @mutex = Support::ReentrantMutex.new
end

Instance Method Details

#fetch_or_store(key) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/rspec/core/memoized_helpers.rb', line 154

def fetch_or_store(key)
  @memoized.fetch(key) do # only first access pays for synchronization
    @mutex.synchronize do
      @memoized.fetch(key) { @memoized[key] = yield }
    end
  end
end