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 mongrel_cluster or Phusion Passenger), 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.

Methods
C
D
I
N
P
Class Public methods
new(options = nil)
    # File activesupport/lib/active_support/cache/memory_store.rb, line 19
19:       def initialize(options = nil)
20:         options ||= {}
21:         super(options)
22:         @data = {}
23:         @key_access = {}
24:         @max_size = options[:size] || 32.megabytes
25:         @max_prune_time = options[:max_prune_time] || 2
26:         @cache_size = 0
27:         @monitor = Monitor.new
28:         @pruning = false
29:       end
Instance Public methods
cleanup(options = nil)
    # File activesupport/lib/active_support/cache/memory_store.rb, line 39
39:       def cleanup(options = nil)
40:         options = merged_options(options)
41:         instrument(:cleanup, :size => @data.size) do
42:           keys = synchronize{ @data.keys }
43:           keys.each do |key|
44:             entry = @data[key]
45:             delete_entry(key, options) if entry && entry.expired?
46:           end
47:         end
48:       end
clear(options = nil)
    # File activesupport/lib/active_support/cache/memory_store.rb, line 31
31:       def clear(options = nil)
32:         synchronize do
33:           @data.clear
34:           @key_access.clear
35:           @cache_size = 0
36:         end
37:       end
decrement(name, amount = 1, options = nil)

Decrement an integer value in the cache.

     # File activesupport/lib/active_support/cache/memory_store.rb, line 90
 90:       def decrement(name, amount = 1, options = nil)
 91:         synchronize do
 92:           options = merged_options(options)
 93:           if num = read(name, options)
 94:             num = num.to_i - amount
 95:             write(name, num, options)
 96:             num
 97:           else
 98:             nil
 99:           end
100:         end
101:       end
delete_matched(matcher, options = nil)
     # File activesupport/lib/active_support/cache/memory_store.rb, line 103
103:       def delete_matched(matcher, options = nil)
104:         options = merged_options(options)
105:         instrument(:delete_matched, matcher.inspect) do
106:           matcher = key_matcher(matcher, options)
107:           keys = synchronize { @data.keys }
108:           keys.each do |key|
109:             delete_entry(key, options) if key.match(matcher)
110:           end
111:         end
112:       end
increment(name, amount = 1, options = nil)

Increment an integer value in the cache.

    # File activesupport/lib/active_support/cache/memory_store.rb, line 76
76:       def increment(name, amount = 1, options = nil)
77:         synchronize do
78:           options = merged_options(options)
79:           if num = read(name, options)
80:             num = num.to_i + amount
81:             write(name, num, options)
82:             num
83:           else
84:             nil
85:           end
86:         end
87:       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 52
52:       def prune(target_size, max_time = nil)
53:         return if pruning?
54:         @pruning = true
55:         begin
56:           start_time = Time.now
57:           cleanup
58:           instrument(:prune, target_size, :from => @cache_size) do
59:             keys = synchronize{ @key_access.keys.sort{|a,b| @key_access[a].to_f <=> @key_access[b].to_f} }
60:             keys.each do |key|
61:               delete_entry(key, options)
62:               return if @cache_size <= target_size || (max_time && Time.now - start_time > max_time)
63:             end
64:           end
65:         ensure
66:           @pruning = false
67:         end
68:       end
pruning?()

Returns true if the cache is currently being pruned.

    # File activesupport/lib/active_support/cache/memory_store.rb, line 71
71:       def pruning?
72:         @pruning
73:       end