Class: I18n::Tasks::Concurrent::CachedValue
- Inherits:
-
Object
- Object
- I18n::Tasks::Concurrent::CachedValue
- Defined in:
- lib/i18n/tasks/concurrent/cached_value.rb
Overview
A thread-safe memoized value. The given computation is guaranteed to be invoked at most once.
Constant Summary collapse
- NULL =
Object.new
Instance Method Summary collapse
-
#get ⇒ Object
Result of the computation.
-
#initialize(&computation) ⇒ CachedValue
constructor
A new instance of CachedValue.
Constructor Details
#initialize(&computation) ⇒ CachedValue
Returns a new instance of CachedValue.
11 12 13 14 15 |
# File 'lib/i18n/tasks/concurrent/cached_value.rb', line 11 def initialize(&computation) @computation = computation @mutex = Mutex.new @result = NULL end |
Instance Method Details
#get ⇒ Object
Returns Result of the computation.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/i18n/tasks/concurrent/cached_value.rb', line 18 def get return get_result_volatile unless get_result_volatile == NULL @mutex.synchronize do next unless get_result_volatile == NULL set_result_volatile @computation.call @computation = nil end get_result_volatile end |