Class: I18n::Tasks::Concurrent::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/tasks/concurrent/cache.rb

Overview

A thread-safe cache.

Since:

  • 0.9.25

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.

Since:

  • 0.9.25



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.

Parameters:

  • key (Object)

Returns:

  • (Object)

    Cached or computed value.

Since:

  • 0.9.25



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