Module: ActiveSupport::Cache::Strategy::LocalCache
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
|
Classes:
| |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Defined in: | activesupport/lib/active_support/cache/strategy/local_cache.rb, activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb |
Overview
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.
Instance Attribute Summary
- #local_cache_key readonly private
Instance Method Summary
- #initialize
-
#local_cache
The current local cache.
-
#middleware
Middlewareclass can be inserted as a::Rackhandler to be local cache for the duration of request. -
#new_local_cache
Set a new local cache.
-
#unset_local_cache
Unset the current local cache.
-
#with_local_cache(&block)
Use a local cache for the duration of block.
- #delete_entry(key) private
-
#delete_multi_entries(entries)
private
Batch deletes may bypass #delete_entry.
- #read_multi_entries(names, **options) private
- #read_multi_entries_from_local_cache(names, options) private
- #read_serialized_entry(key, raw: false, **options) private
- #use_temporary_local_cache(temporary_cache) private
- #write_cache_value(name, value, **options) private
-
#write_multi_entries(entries)
private
Batch writes may bypass #write_serialized_entry.
- #write_serialized_entry(key, payload) private
- #cleanup(options = nil) Internal use only
- #clear(options = nil) Internal use only
- #decrement(name, amount = 1, **options) Internal use only
- #delete_matched(matcher, options = nil) Internal use only
- #fetch_multi(*names, &block) Internal use only
- #increment(name, amount = 1, **options) Internal use only
Instance Attribute Details
#local_cache_key (readonly, private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 147
attr_reader :local_cache_key
Instance Method Details
#cleanup(options = nil)
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 105
def cleanup( = nil) # :nodoc: return super unless cache = local_cache cache.clear() super end
#clear(options = nil)
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 99
def clear( = nil) # :nodoc: return super unless cache = local_cache cache.clear() super end
#decrement(name, amount = 1, **options)
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 124
def decrement(name, amount = 1, **) # :nodoc: return super unless local_cache value = super write_cache_value(name, value, raw: true, **) value end
#delete_entry(key) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 221
def delete_entry(key, **) local_cache.delete_entry(key) if local_cache super end
#delete_matched(matcher, options = nil)
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 111
def delete_matched(matcher, = nil) # :nodoc: return super unless cache = local_cache cache.clear() super end
#delete_multi_entries(entries) (private)
Batch deletes may bypass #delete_entry.
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 227
def delete_multi_entries(entries, **) if cache = local_cache entries.each { |key| cache.delete_entry(key) } end super end
#fetch_multi(*names, &block)
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 131
def fetch_multi(*names, &block) # :nodoc: return super if local_cache.nil? || names.empty? = names. = () results = [:force] ? {} : read_multi_entries_from_local_cache(names, ) if results.size < names.size results.merge!(super(*(names - results.keys), , &block)) end results.slice(*names) end
#increment(name, amount = 1, **options)
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 117
def increment(name, amount = 1, **) # :nodoc: return super unless local_cache value = super write_cache_value(name, value, raw: true, **) value end
#initialize
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 68
def initialize(...) super @local_cache_key = :"local_cache_#{object_id}" end
#local_cache
The current local cache.
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 89
def local_cache LocalCacheRegistry.cache_for(local_cache_key) end
#middleware
LocalCache::Middleware class can be inserted as a ::Rack handler to be local cache for the
duration of request.
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 95
def middleware @middleware ||= Middleware.new("ActiveSupport::Cache::Strategy::LocalCache", self) end
#new_local_cache
Set a new local cache.
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 79
def new_local_cache LocalCacheRegistry.set_cache_for(local_cache_key, LocalStore.new) end
#read_multi_entries(names, **options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 192
def read_multi_entries(names, **) return super unless local_cache results = read_multi_entries_from_local_cache(names, ) if results.size < names.size results.merge!(super(names - results.keys, **)) end results.slice(*names) end
#read_multi_entries_from_local_cache(names, options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 169
def read_multi_entries_from_local_cache(names, ) keys_to_names = names.index_by { |name| normalize_key(name, ) } local_entries = local_cache.read_multi_entries(keys_to_names.keys) local_entries.each_with_object({}) do |(key, value), result| # If we recorded a miss in the local cache, the caller will forward # that key to the real store, and the entry will be replaced next if value.nil? entry = deserialize_entry(value, **) normalized_key = keys_to_names[key] if entry.nil? result[normalized_key] = nil elsif entry.expired? || entry.mismatched?(normalize_version(normalized_key, )) local_cache.delete_entry(key) else result[normalized_key] = entry.value end end end
#read_serialized_entry(key, raw: false, **options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 149
def read_serialized_entry(key, raw: false, **) if cache = local_cache if [:delete] entry = super cache.delete_entry(key) entry else hit = true entry = cache.fetch_entry(key) do hit = false super end [:event][:store] = cache.class.name if hit && [:event] entry end else super end end
#unset_local_cache
Unset the current local cache.
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 84
def unset_local_cache LocalCacheRegistry.set_cache_for(local_cache_key, nil) end
#use_temporary_local_cache(temporary_cache) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 244
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
#with_local_cache(&block)
Use a local cache for the duration of block.
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 74
def with_local_cache(&block) use_temporary_local_cache(LocalStore.new, &block) end
#write_cache_value(name, value, **options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 234
def write_cache_value(name, value, **) name = normalize_key(name, ) cache = local_cache if value cache.write_entry(name, serialize_entry(new_entry(value, **), **)) else cache.delete_entry(name) end end
#write_multi_entries(entries) (private)
Batch writes may bypass #write_serialized_entry.
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 214
def write_multi_entries(entries, **) if cache = local_cache entries.each_key { |key| cache.delete_entry(key) } end super end
#write_serialized_entry(key, payload) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 204
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