Class: ActiveSupport::Cache::MemoryStore

Inherits:
Store show all
Defined in:
lib/active_support/cache/memory_store.rb

Overview

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.

MemoryStore is thread-safe.

Instance Attribute Summary

Attributes inherited from Store

#options, #silence

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Store

#delete, #exist?, #fetch, #fetch_multi, #mute, #read, #read_multi, #silence!, #write, #write_multi

Constructor Details

#initialize(options = nil) ⇒ MemoryStore

Returns a new instance of MemoryStore.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_support/cache/memory_store.rb', line 21

def initialize(options = nil)
  options ||= {}
  super(options)
  @data = {}
  @key_access = {}
  @max_size = options[:size] || 32.megabytes
  @max_prune_time = options[:max_prune_time] || 2
  @cache_size = 0
  @monitor = Monitor.new
  @pruning = false
end

Class Method Details

.supports_cache_versioning?Boolean

Advertise cache versioning support.

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_support/cache/memory_store.rb', line 34

def self.supports_cache_versioning?
  true
end

Instance Method Details

#cleanup(options = nil) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/active_support/cache/memory_store.rb', line 48

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) ⇒ Object

Delete all data stored in a given cache store.



39
40
41
42
43
44
45
# File 'lib/active_support/cache/memory_store.rb', line 39

def clear(options = nil)
  synchronize do
    @data.clear
    @key_access.clear
    @cache_size = 0
  end
end

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

Decrement an integer value in the cache.



90
91
92
# File 'lib/active_support/cache/memory_store.rb', line 90

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

#delete_matched(matcher, options = nil) ⇒ Object

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



95
96
97
98
99
100
101
102
103
104
# File 'lib/active_support/cache/memory_store.rb', line 95

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

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

Increment an integer value in the cache.



85
86
87
# File 'lib/active_support/cache/memory_store.rb', line 85

def increment(name, amount = 1, options = nil)
  modify_value(name, amount, options)
end

#inspectObject

:nodoc:



106
107
108
# File 'lib/active_support/cache/memory_store.rb', line 106

def inspect # :nodoc:
  "<##{self.class.name} entries=#{@data.size}, size=#{@cache_size}, options=#{@options.inspect}>"
end

#prune(target_size, max_time = nil) ⇒ Object

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_support/cache/memory_store.rb', line 61

def prune(target_size, max_time = nil)
  return if pruning?
  @pruning = true
  begin
    start_time = Concurrent.monotonic_time
    cleanup
    instrument(:prune, target_size, from: @cache_size) do
      keys = synchronize { @key_access.keys.sort { |a, b| @key_access[a].to_f <=> @key_access[b].to_f } }
      keys.each do |key|
        delete_entry(key, **options)
        return if @cache_size <= target_size || (max_time && Concurrent.monotonic_time - start_time > max_time)
      end
    end
  ensure
    @pruning = false
  end
end

#pruning?Boolean

Returns true if the cache is currently being pruned.

Returns:

  • (Boolean)


80
81
82
# File 'lib/active_support/cache/memory_store.rb', line 80

def pruning?
  @pruning
end

#synchronize(&block) ⇒ Object

Synchronize calls to the cache. This should be called wherever the underlying cache implementation is not thread safe.



112
113
114
# File 'lib/active_support/cache/memory_store.rb', line 112

def synchronize(&block) # :nodoc:
  @monitor.synchronize(&block)
end