Class: ActiveSupport::Cache::MemoryStore
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Store
|
|
Instance Chain:
self,
Store
|
|
Inherits: |
ActiveSupport::Cache::Store
|
Defined in: | activesupport/lib/active_support/cache/memory_store.rb |
Overview
Memory Cache Store
A cache store implementation which stores everything into memory in the same process. If you’re running multiple Ruby on Rails server processes (which is the case if you’re using Phusion Passenger or puma clustered mode), then this means that Rails server process instances won’t be able to share cache data with each other and this may not be the most appropriate cache in that scenario.
This cache has a bounded size specified by the :size
options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur which tries to prune the cache down to three quarters of the maximum size by removing the least recently used entries.
Unlike other ::ActiveSupport::Cache
store implementations, MemoryStore
does not compress values by default. MemoryStore
does not benefit from compression as much as other Store
implementations, as it does not send data over a network. However, when compression is enabled, it still pays the full cost of compression in terms of cpu use.
MemoryStore
is thread-safe.
Constant Summary
-
PER_ENTRY_OVERHEAD =
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 196240
Store
- Inherited
Class Attribute Summary
-
.supports_cache_versioning? ⇒ Boolean
readonly
Advertise cache versioning support.
Store
- Inherited
Class Method Summary
- .new(options = nil) ⇒ MemoryStore constructor
Store
- Inherited
.new | Creates a new cache. |
.retrieve_pool_options |
Instance Attribute Summary
-
#pruning? ⇒ Boolean
readonly
Returns true if the cache is currently being pruned.
Store
- Inherited
Instance Method Summary
-
#cleanup(options = nil)
Preemptively iterates through all stored keys and removes the ones which have expired.
-
#clear(options = nil)
Delete all data stored in a given cache store.
-
#decrement(name, amount = 1, options = nil)
Decrement a cached integer value.
-
#delete_matched(matcher, options = nil)
Deletes cache entries if the cache key matches a given pattern.
-
#increment(name, amount = 1, options = nil)
Increment a cached integer value.
-
#prune(target_size, max_time = nil)
To ensure entries fit within the specified memory prune the cache by removing the least recently accessed entries.
- #cached_size(key, payload) private
- #delete_entry(key, **options) private
-
#modify_value(name, amount, options)
private
Modifies the amount of an integer value that is stored in the cache.
- #read_entry(key, **options) private
- #write_entry(key, entry, **options) private
- #inspect Internal use only
-
#synchronize(&block)
Internal use only
Synchronize calls to the cache.
Store
- Inherited
#cleanup | Cleans up the cache by removing expired entries. |
#clear | Clears the entire cache. |
#decrement | Decrements an integer value in the cache. |
#delete | Deletes an entry in the cache. |
#delete_matched | Deletes all entries with keys matching the pattern. |
#delete_multi | Deletes multiple entries in the cache. |
#exist? | Returns |
#fetch | Fetches data from the cache, using the given key. |
#fetch_multi | Fetches data from the cache, using the given keys. |
#increment | Increments an integer value in the cache. |
#mute | Silences the logger within a block. |
#read | Reads data from the cache, using the given key. |
#read_multi | Reads multiple values at once from the cache. |
#silence, | |
#silence! | Silences the logger. |
#write | Writes the value to the cache with the key. |
#write_multi |
|
#_instrument, #default_serializer, | |
#delete_entry | Deletes an entry from the cache implementation. |
#delete_multi_entries | Deletes multiples entries in the cache implementation. |
#deserialize_entry, | |
#expanded_key | Expands key to be a consistent string value. |
#expanded_version, #get_entry_value, #handle_expired_entry, #handle_invalid_expires_in, #instrument, #instrument_multi, | |
#key_matcher | Adds the namespace defined in the options to a pattern designed to match keys. |
#merged_options | Merges the default options with ones specific to a method call. |
#namespace_key | Prefix the key with a namespace string: |
#normalize_key | Expands and namespaces the cache key. |
#normalize_options | Normalize aliased options to their canonical form. |
#normalize_version, | |
#read_entry | Reads an entry from the cache implementation. |
#read_multi_entries | Reads multiple entries from the cache implementation. |
#save_block_result_to_cache, #serialize_entry, #validate_options, | |
#write_entry | Writes an entry to the cache implementation. |
#write_multi_entries | Writes multiple entries to the cache implementation. |
#new_entry |
Constructor Details
.new(options = nil) ⇒ MemoryStore
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 73
def initialize( = nil) ||= {} [:coder] = DupCoder unless .key?(:coder) || .key?(:serializer) # Disable compression by default. [:compress] ||= false super( ) @data = {} @max_size = [:size] || 32.megabytes @max_prune_time = [:max_prune_time] || 2 @cache_size = 0 @monitor = Monitor.new @pruning = false end
Class Attribute Details
.supports_cache_versioning? ⇒ Boolean
(readonly)
Advertise cache versioning support.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 88
def self.supports_cache_versioning? true end
Instance Attribute Details
#pruning? ⇒ Boolean
(readonly)
Returns true if the cache is currently being pruned.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 133
def pruning? @pruning end
Instance Method Details
#cached_size(key, payload) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/memory_store.rb', line 198
def cached_size(key, payload) key.to_s.bytesize + payload.bytesize + PER_ENTRY_OVERHEAD end
#cleanup(options = nil)
Preemptively iterates through all stored keys and removes the ones which have expired.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 101
def cleanup( = nil) = ( ) _instrument(:cleanup, size: @data.size) do keys = synchronize { @data.keys } keys.each do |key| entry = @data[key] delete_entry(key, ** ) if entry && entry.expired? end end end
#clear(options = nil)
Delete all data stored in a given cache store.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 93
def clear( = nil) synchronize do @data.clear @cache_size = 0 end end
#decrement(name, amount = 1, options = nil)
Decrement a cached integer value. Returns the updated value.
If the key is unset or has expired, it will be set to -amount
.
cache.decrement("foo") # => -1
To set a specific value, call #write
:
cache.write("baz", 5)
cache.decrement("baz") # => 4
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 166
def decrement(name, amount = 1, = nil) instrument(:decrement, name, amount: amount) do modify_value(name, -amount, ) end end
#delete_entry(key, **options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/memory_store.rb', line 231
def delete_entry(key, ** ) synchronize do payload = @data.delete(key) @cache_size -= cached_size(key, payload) if payload !!payload end end
#delete_matched(matcher, options = nil)
Deletes cache entries if the cache key matches a given pattern.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 173
def delete_matched(matcher, = nil) = ( ) matcher = key_matcher(matcher, ) instrument(:delete_matched, matcher.inspect) do keys = synchronize { @data.keys } keys.each do |key| delete_entry(key, ** ) if key.match(matcher) end end end
#increment(name, amount = 1, options = nil)
Increment a cached integer value. Returns the updated value.
If the key is unset, it will be set to amount
:
cache.increment("foo") # => 1
cache.increment("bar", 100) # => 100
To set a specific value, call #write
:
cache.write("baz", 5)
cache.increment("baz") # => 6
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 149
def increment(name, amount = 1, = nil) instrument(:increment, name, amount: amount) do modify_value(name, amount, ) end end
#inspect
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 185
def inspect # :nodoc: "#<#{self.class.name} entries=#{@data.size}, size=#{@cache_size}, options=#{@options.inspect}>" end
#modify_value(name, amount, options) (private)
Modifies the amount of an integer value that is stored in the cache. If the key is not found it is created and set to amount
.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 241
def modify_value(name, amount, ) = ( ) key = normalize_key(name, ) version = normalize_version(name, ) synchronize do entry = read_entry(key, ** ) if !entry || entry.expired? || entry.mismatched?(version) write(name, Integer(amount), ) amount else num = entry.value.to_i + amount entry = Entry.new(num, expires_at: entry.expires_at, version: entry.version) write_entry(key, entry) num end end end
#prune(target_size, max_time = nil)
To ensure entries fit within the specified memory prune the cache by removing the least recently accessed entries.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 114
def prune(target_size, max_time = nil) return if pruning? @pruning = true begin start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) cleanup instrument(:prune, target_size, from: @cache_size) do keys = synchronize { @data.keys } keys.each do |key| delete_entry(key, ** ) return if @cache_size <= target_size || (max_time && Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time > max_time) end end ensure @pruning = false end end
#read_entry(key, **options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/memory_store.rb', line 202
def read_entry(key, ** ) entry = nil synchronize do payload = @data.delete(key) if payload @data[key] = payload entry = deserialize_entry(payload) end end entry end
#synchronize(&block)
Synchronize calls to the cache. This should be called wherever the underlying cache implementation is not thread safe.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 191
def synchronize(&block) # :nodoc: @monitor.synchronize(&block) end
#write_entry(key, entry, **options) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache/memory_store.rb', line 214
def write_entry(key, entry, ** ) payload = serialize_entry(entry, ** ) synchronize do return false if [:unless_exist] && exist?(key, namespace: nil) old_payload = @data[key] if old_payload @cache_size -= (old_payload.bytesize - payload.bytesize) else @cache_size += cached_size(key, payload) end @data[key] = payload prune(@max_size * 0.75, @max_prune_time) if @cache_size > @max_size true end end