Module: ActiveSupport::Cache::Strategy::LocalCache

Included in:
MemCacheStore, NullStore, RedisCacheStore
Defined in:
lib/active_support/cache/strategy/local_cache.rb,
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.

Defined Under Namespace

Modules: LocalCacheRegistry Classes: LocalStore, Middleware

Instance Method Summary collapse

Instance Method Details

#cleanup(options = nil) ⇒ Object

:nodoc:



98
99
100
101
102
# File 'lib/active_support/cache/strategy/local_cache.rb', line 98

def cleanup(options = nil) # :nodoc:
  return super unless cache = local_cache
  cache.clear(options)
  super
end

#clear(options = nil) ⇒ Object

:nodoc:



92
93
94
95
96
# File 'lib/active_support/cache/strategy/local_cache.rb', line 92

def clear(options = nil) # :nodoc:
  return super unless cache = local_cache
  cache.clear(options)
  super
end

#decrement(name, amount = 1, **options) ⇒ Object

:nodoc:



117
118
119
120
121
122
# File 'lib/active_support/cache/strategy/local_cache.rb', line 117

def decrement(name, amount = 1, **options) # :nodoc:
  return super unless local_cache
  value = bypass_local_cache { super }
  write_cache_value(name, value, raw: true, **options)
  value
end

#delete_matched(matcher, options = nil) ⇒ Object

:nodoc:



104
105
106
107
108
# File 'lib/active_support/cache/strategy/local_cache.rb', line 104

def delete_matched(matcher, options = nil) # :nodoc:
  return super unless cache = local_cache
  cache.clear(options)
  super
end

#fetch_multi(*names, &block) ⇒ Object

:nodoc:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/active_support/cache/strategy/local_cache.rb', line 124

def fetch_multi(*names, &block) # :nodoc:
  return super if local_cache.nil? || names.empty?

  options = names.extract_options!
  options = merged_options(options)

  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|
    # If we recorded a miss in the local cache, `#fetch_multi` will forward
    # that key to the real store, and the entry will be replaced
    # local_cache.delete_entry(key)
    next if value.nil?

    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, &block))
  end

  results
end

#increment(name, amount = 1, **options) ⇒ Object

:nodoc:



110
111
112
113
114
115
# File 'lib/active_support/cache/strategy/local_cache.rb', line 110

def increment(name, amount = 1, **options) # :nodoc:
  return super unless local_cache
  value = bypass_local_cache { super }
  write_cache_value(name, value, raw: true, **options)
  value
end

#local_cacheObject

The current local cache.



82
83
84
# File 'lib/active_support/cache/strategy/local_cache.rb', line 82

def local_cache
  LocalCacheRegistry.cache_for(local_cache_key)
end

#middlewareObject

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



88
89
90
# File 'lib/active_support/cache/strategy/local_cache.rb', line 88

def middleware
  @middleware ||= Middleware.new("ActiveSupport::Cache::Strategy::LocalCache", self)
end

#new_local_cacheObject

Set a new local cache.



72
73
74
# File 'lib/active_support/cache/strategy/local_cache.rb', line 72

def new_local_cache
  LocalCacheRegistry.set_cache_for(local_cache_key, LocalStore.new)
end

#unset_local_cacheObject

Unset the current local cache.



77
78
79
# File 'lib/active_support/cache/strategy/local_cache.rb', line 77

def unset_local_cache
  LocalCacheRegistry.set_cache_for(local_cache_key, nil)
end

#with_local_cache(&block) ⇒ Object

Use a local cache for the duration of block.



67
68
69
# File 'lib/active_support/cache/strategy/local_cache.rb', line 67

def with_local_cache(&block)
  use_temporary_local_cache(LocalStore.new, &block)
end