module ActiveSupport::Cache::Strategy::LocalCache

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 89
def middleware
  @middleware ||= Middleware.new(
    "ActiveSupport::Cache::Strategy::LocalCache",
    local_cache_key)
end
with_local_cache() { || ... } click to toggle source

Use a local cache for the duration of block.

# File lib/active_support/cache/strategy/local_cache.rb, line 83
def with_local_cache
  use_temporary_local_cache(LocalStore.new) { yield }
end

Private Instance Methods

bypass_local_cache() { || ... } click to toggle source
# File lib/active_support/cache/strategy/local_cache.rb, line 178
def bypass_local_cache
  use_temporary_local_cache(nil) { yield }
end
delete_entry(key, options) click to toggle source
Calls superclass method
# File lib/active_support/cache/strategy/local_cache.rb, line 153
def delete_entry(key, options)
  local_cache.delete_entry(key, options) if local_cache
  super
end
local_cache() click to toggle source
# File lib/active_support/cache/strategy/local_cache.rb, line 174
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 170
def local_cache_key
  @local_cache_key ||= "#{self.class.name.underscore}_local_cache_#{object_id}".gsub(/[\/-]/, "_").to_sym
end
read_entry(key, options) click to toggle source
Calls superclass method
# File lib/active_support/cache/strategy/local_cache.rb, line 122
def read_entry(key, options)
  if cache = local_cache
    cache.fetch_entry(key) { super }
  else
    super
  end
end
read_multi_entries(keys, options) click to toggle source
Calls superclass method
# File lib/active_support/cache/strategy/local_cache.rb, line 130
def read_multi_entries(keys, options)
  return super unless local_cache

  local_entries = local_cache.read_multi_entries(keys, options)
  missed_keys = keys - local_entries.keys

  if missed_keys.any?
    local_entries.merge!(super(missed_keys, options))
  else
    local_entries
  end
end
use_temporary_local_cache(temporary_cache) { || ... } click to toggle source
# File lib/active_support/cache/strategy/local_cache.rb, line 182
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 158
def write_cache_value(name, value, options)
  name = normalize_key(name, options)
  cache = local_cache
  cache.mute do
    if value
      cache.write(name, value, options)
    else
      cache.delete(name, options)
    end
  end
end
write_entry(key, entry, options) click to toggle source
Calls superclass method
# File lib/active_support/cache/strategy/local_cache.rb, line 143
def write_entry(key, entry, options)
  if options[:unless_exist]
    local_cache.delete_entry(key, options) if local_cache
  else
    local_cache.write_entry(key, entry, options) if local_cache
  end

  super
end