123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::Cache::MemoryStore

Relationships & Source Files
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

Store - Inherited

DEFAULT_POOL_OPTIONS

Class Attribute Summary

Store - Inherited

Class Method Summary

Store - Inherited

.new

Creates a new cache.

Instance Attribute Summary

Store - Inherited

Instance Method Summary

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 true if the cache contains an entry for the given key.

#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

::ActiveSupport::Cache Storage API to write multiple values at once.

#key_matcher

Adds the namespace defined in the options to a pattern designed to match keys.

Constructor Details

.new(options = nil) ⇒ MemoryStore

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 73

def initialize(options = nil)
  options ||= {}
  options[:coder] = DupCoder unless options.key?(:coder) || options.key?(:serializer)
  # 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

Class Attribute Details

.supports_cache_versioning?Boolean (readonly)

Advertise cache versioning support.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 133

def pruning?
  @pruning
end

Instance Method Details

#cleanup(options = nil)

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 101

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

#clear(options = nil)

Delete all data stored in a given cache store.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 93

def clear(options = 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
[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 166

def decrement(name, amount = 1, options = nil)
  instrument(:decrement, name, amount: amount) do
    modify_value(name, -amount, options)
  end
end

#delete_matched(matcher, options = nil)

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 173

def delete_matched(matcher, options = nil)
  options = merged_options(options)
  matcher = key_matcher(matcher, options)

  instrument(:delete_matched, matcher.inspect) do
    keys = synchronize { @data.keys }
    keys.each do |key|
      delete_entry(key, **options) 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
[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 149

def increment(name, amount = 1, options = nil)
  instrument(:increment, name, amount: amount) do
    modify_value(name, amount, options)
  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.

[ GitHub ]

  
# 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, **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