Class: I18n::Tasks::Concurrent::Cache
- Inherits:
-
Object
- Object
- I18n::Tasks::Concurrent::Cache
- Defined in:
- lib/i18n/tasks/concurrent/cache.rb
Overview
A thread-safe cache.
Instance Method Summary collapse
-
#fetch(key, &block) ⇒ Object
Cached or computed value.
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
9 10 11 12 |
# File 'lib/i18n/tasks/concurrent/cache.rb', line 9 def initialize @mutex = Mutex.new @map = {} end |
Instance Method Details
#fetch(key, &block) ⇒ Object
Returns Cached or computed value.
16 17 18 19 20 |
# File 'lib/i18n/tasks/concurrent/cache.rb', line 16 def fetch(key, &block) @mutex.synchronize do @map[key] ||= CachedValue.new(&block) end.get end |