Class: MultiJson::OptionsCache::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_json/options_cache.rb

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



12
13
14
15
# File 'lib/multi_json/options_cache.rb', line 12

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

Instance Method Details

#fetch(key, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/multi_json/options_cache.rb', line 23

def fetch(key, &block)
  @mutex.synchronize do
    return @cache[key] if @cache.key?(key)
  end

  value = yield

  @mutex.synchronize do
    if @cache.key?(key)
      # We ran into a race condition, keep the existing value
      @cache[key]
    else
      @cache.clear if @cache.size >= MAX_CACHE_SIZE
      @cache[key] = value
    end
  end
end

#resetObject



17
18
19
20
21
# File 'lib/multi_json/options_cache.rb', line 17

def reset
  @mutex.synchronize do
    @cache = {}
  end
end