module ActiveSupport::Cache::Strategy::LocalCache

Local Cache Strategy ↑

Caches that implement LocalCache will be backed by an in-memory cache for the duration of a block. Repeated calls to the cache for the same key will hit the in-memory cache for faster access.

Public Instance Methods

middleware() click to toggle source

Middleware class can be inserted as a Rack handler to be local cache for the duration of request.

# File lib/active_support/cache/strategy/local_cache.rb, line 73
def middleware
  @middleware ||= Middleware.new(
    "ActiveSupport::Cache::Strategy::LocalCache",
    local_cache_key)
end
with_local_cache(&block) click to toggle source

Use a local cache for the duration of block.

# File lib/active_support/cache/strategy/local_cache.rb, line 67
def with_local_cache(&block)
  use_temporary_local_cache(LocalStore.new, &block)
end

Private Instance Methods

bypass_local_cache(&block) click to toggle source
# File lib/active_support/cache/strategy/local_cache.rb, line 221
def bypass_local_cache(&block)
  use_temporary_local_cache(nil, &block)
end
delete_entry(key, **) click to toggle source
Calls superclass method
# File lib/active_support/cache/strategy/local_cache.rb, line 198
def delete_entry(key, **)
  local_cache.delete_entry(key) if local_cache
  super
end
local_cache() click to toggle source
# File lib/active_support/cache/strategy/local_cache.rb, line 217
def local_cache
  LocalCacheRegistry.cache_for(local_cache_key)
end
local_cache_key() click to toggle source
# File lib/active_support/cache/strategy/local_cache.rb, line 213
def local_cache_key
  @local_cache_key ||= "#{self.class.name.underscore}_local_cache_#{object_id}".gsub(/[\/-]/, "_").to_sym
end
read_multi_entries(names, **options) click to toggle source
Calls superclass method
# File lib/active_support/cache/strategy/local_cache.rb, line 160
def read_multi_entries(names, **options)
  return super unless local_cache

  keys_to_names = names.index_by { |name| normalize_key(name, options) }

  local_entries = local_cache.read_multi_entries(keys_to_names.keys)

  results = local_entries.each_with_object({}) do |(key, value), result|
    next if value.nil? # recorded cache miss

    entry = deserialize_entry(value, **options)

    normalized_key = keys_to_names[key]
    if entry.nil?
      result[normalized_key] = nil
    elsif entry.expired? || entry.mismatched?(normalize_version(normalized_key, options))
      local_cache.delete_entry(key)
    else
      result[normalized_key] = entry.value
    end
  end

  if results.size < names.size
    results.merge!(super(names - results.keys, **options))
  end

  results
end
read_serialized_entry(key, raw: false, **options) click to toggle source
Calls superclass method
# File lib/active_support/cache/strategy/local_cache.rb, line 146
def read_serialized_entry(key, raw: false, **options)
  if cache = local_cache
    hit = true
    entry = cache.fetch_entry(key) do
      hit = false
      super
    end
    options[:event][:store] = cache.class.name if hit && options[:event]
    entry
  else
    super
  end
end
use_temporary_local_cache(temporary_cache) { || ... } click to toggle source
# File lib/active_support/cache/strategy/local_cache.rb, line 225
def use_temporary_local_cache(temporary_cache)
  save_cache = LocalCacheRegistry.cache_for(local_cache_key)
  begin
    LocalCacheRegistry.set_cache_for(local_cache_key, temporary_cache)
    yield
  ensure
    LocalCacheRegistry.set_cache_for(local_cache_key, save_cache)
  end
end
write_cache_value(name, value, **options) click to toggle source
# File lib/active_support/cache/strategy/local_cache.rb, line 203
def write_cache_value(name, value, **options)
  name = normalize_key(name, options)
  cache = local_cache
  if value
    cache.write_entry(name, serialize_entry(new_entry(value, **options), **options))
  else
    cache.delete_entry(name)
  end
end
write_serialized_entry(key, payload, **) click to toggle source
Calls superclass method
# File lib/active_support/cache/strategy/local_cache.rb, line 189
def write_serialized_entry(key, payload, **)
  if return_value = super
    local_cache.write_entry(key, payload) if local_cache
  else
    local_cache.delete_entry(key) if local_cache
  end
  return_value
end