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 Method Summary
-
#middleware
Middleware
class can be inserted as a::Rack
handler to be local cache for the duration of request. -
#with_local_cache(&block)
Use a local cache for the duration of block.
- #bypass_local_cache(&block) private
- #delete_entry(key) private
- #local_cache private
- #local_cache_key private
- #read_multi_entries(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_serialized_entry(key, payload) private
- #cleanup(options = nil) Internal use only
- #clear(options = nil) Internal use only
- #decrement(name, amount = 1, options = nil) Internal use only
- #delete_matched(matcher, options = nil) Internal use only
- #increment(name, amount = 1, options = nil) Internal use only
Instance Method Details
#bypass_local_cache(&block) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 185
def bypass_local_cache(&block) use_temporary_local_cache(nil, &block) end
#cleanup(options = nil)
This method is for internal use only.
[ GitHub ]
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 85
def cleanup( = nil) # :nodoc: return super unless cache = local_cache cache.clear( ) super end
#clear(options = nil)
This method is for internal use only.
[ GitHub ]
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 79
def clear( = nil) # :nodoc: return super unless cache = local_cache cache.clear( ) super end
#decrement(name, amount = 1, options = nil)
This method is for internal use only.
[ GitHub ]
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 108
def decrement(name, amount = 1, = nil) # :nodoc: return super unless local_cache value = bypass_local_cache { super } if write_cache_value(name, value, raw: true, ** ) else write_cache_value(name, value, raw: true) end value end
#delete_entry(key) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 162
def delete_entry(key, **) local_cache.delete_entry(key) if local_cache super end
#delete_matched(matcher, options = nil)
This method is for internal use only.
[ GitHub ]
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 91
def delete_matched(matcher, = nil) # :nodoc: return super unless cache = local_cache cache.clear( ) super end
#increment(name, amount = 1, options = nil)
This method is for internal use only.
[ GitHub ]
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 97
def increment(name, amount = 1, = nil) # :nodoc: return super unless local_cache value = bypass_local_cache { super } if write_cache_value(name, value, raw: true, ** ) else write_cache_value(name, value, raw: true) end value end
#local_cache (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 181
def local_cache LocalCacheRegistry.cache_for(local_cache_key) end
#local_cache_key (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 177
def local_cache_key @local_cache_key ||= "#{self.class.name.underscore}_local_cache_#{object_id}".gsub(/[\/-]/, "_").to_sym 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 73
def middleware @middleware ||= Middleware.new( "ActiveSupport::Cache::Strategy::LocalCache", local_cache_key) end
#read_multi_entries(names, **options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 134
def read_multi_entries(names, ** ) return super unless local_cache keys_to_names = names.index_by { |name| normalize_key(name, ) } local_entries = local_cache.read_multi_entries(keys_to_names.keys) local_entries.transform_keys! { |key| keys_to_names[key] } local_entries.transform_values! do |payload| deserialize_entry(payload, ** )&.value end missed_names = names - local_entries.keys if missed_names.any? local_entries.merge!(super(missed_names, ** )) else local_entries end end
#read_serialized_entry(key, raw: false, **options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 120
def read_serialized_entry(key, raw: false, ** ) if cache = local_cache hit = true entry = cache.fetch_entry(key) do hit = false super end [:event][:store] = cache.class.name if hit && [:event] entry else super end end
#use_temporary_local_cache(temporary_cache) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 189
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 67
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 167
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_serialized_entry(key, payload) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 153
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