W3cubDocs

/Ruby on Rails 7.0

class ActiveSupport::Cache::MemoryStore

Parent:
ActiveSupport::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 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.

Constants

PER_ENTRY_OVERHEAD

Public Class Methods

new(options = nil) Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 48
def initialize(options = nil)
  options ||= {}
  # Disable compression by default.
  options[:compress] ||= false
  super(options)
  @data = {}
  @max_size = options[:size] || 32.megabytes
  @max_prune_time = options[:max_prune_time] || 2
  @cache_size = 0
  @monitor = Monitor.new
  @pruning = false
end
Calls superclass method ActiveSupport::Cache::Store::new
supports_cache_versioning?() Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 62
def self.supports_cache_versioning?
  true
end

Advertise cache versioning support.

Public Instance Methods

cleanup(options = nil) Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 75
def cleanup(options = nil)
  options = merged_options(options)
  instrument(:cleanup, size: @data.size) do
    keys = synchronize { @data.keys }
    keys.each do |key|
      entry = @data[key]
      delete_entry(key, **options) if entry && entry.expired?
    end
  end
end

Preemptively iterates through all stored keys and removes the ones which have expired.

clear(options = nil) Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 67
def clear(options = nil)
  synchronize do
    @data.clear
    @cache_size = 0
  end
end

Delete all data stored in a given cache store.

decrement(name, amount = 1, options = nil) Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 117
def decrement(name, amount = 1, options = nil)
  modify_value(name, -amount, options)
end

Decrement an integer value in the cache.

delete_matched(matcher, options = nil) Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 122
def delete_matched(matcher, options = nil)
  options = merged_options(options)
  instrument(:delete_matched, matcher.inspect) do
    matcher = key_matcher(matcher, options)
    keys = synchronize { @data.keys }
    keys.each do |key|
      delete_entry(key, **options) if key.match(matcher)
    end
  end
end

Deletes cache entries if the cache key matches a given pattern.

increment(name, amount = 1, options = nil) Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 112
def increment(name, amount = 1, options = nil)
  modify_value(name, amount, options)
end

Increment an integer value in the cache.

prune(target_size, max_time = nil) Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 88
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, **options)
        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

To ensure entries fit within the specified memory prune the cache by removing the least recently accessed entries.

pruning?() Show source
# File activesupport/lib/active_support/cache/memory_store.rb, line 107
def pruning?
  @pruning
end

Returns true if the cache is currently being pruned.

© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.