Class: MultiJson::OptionsCache::Store Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Thread-safe cache store using double-checked locking pattern

Constant Summary collapse

NOT_FOUND =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Sentinel value to detect cache misses (unique object identity)

Object.new

Instance Method Summary collapse

Constructor Details

#initializeStore

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new cache store



24
25
26
27
# File 'lib/multi_json/options_cache.rb', line 24

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

Instance Method Details

#fetch(key, default = nil) { ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fetch a value from cache or compute it

Parameters:

  • key (Object)

    cache key

  • default (Object) (defaults to: nil)

    default value if key not found

Yields:

  • block to compute value if not cached

Returns:

  • (Object)

    cached or computed value



44
45
46
47
48
49
50
51
52
53
# File 'lib/multi_json/options_cache.rb', line 44

def fetch(key, default = nil)
  # Fast path: check cache without lock (safe for reads)
  value = @cache.fetch(key, NOT_FOUND)
  return value unless value.equal?(NOT_FOUND)

  # Slow path: acquire lock and compute value
  @mutex.synchronize do
    @cache.fetch(key) { block_given? ? store(key, yield) : default }
  end
end

#resetvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Clear all cached entries



33
34
35
# File 'lib/multi_json/options_cache.rb', line 33

def reset
  @mutex.synchronize { @cache.clear }
end