Class: ActiveSupport::Cache::MemoryStore
- 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 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.
Instance Attribute Summary
Attributes inherited from Store
Instance Method Summary collapse
-
#cleanup(options = nil) ⇒ Object
Preemptively iterates through all stored keys and removes the ones which have expired.
- #clear(options = nil) ⇒ Object
-
#decrement(name, amount = 1, options = nil) ⇒ Object
Decrement an integer value in the cache.
- #delete_matched(matcher, options = nil) ⇒ Object
-
#increment(name, amount = 1, options = nil) ⇒ Object
Increment an integer value in the cache.
-
#initialize(options = nil) ⇒ MemoryStore
constructor
A new instance of MemoryStore.
-
#inspect ⇒ Object
:nodoc:.
-
#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.
-
#pruning? ⇒ Boolean
Returns true if the cache is currently being pruned.
-
#synchronize(&block) ⇒ Object
Synchronize calls to the cache.
Methods inherited from Store
#delete, #exist?, #fetch, #fetch_multi, #mute, #read, #read_multi, #silence!, #write
Constructor Details
#initialize(options = nil) ⇒ MemoryStore
Returns a new instance of MemoryStore.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/active_support/cache/memory_store.rb', line 19 def initialize( = nil) ||= {} super() @data = {} @key_access = {} @max_size = [:size] || 32.megabytes @max_prune_time = [:max_prune_time] || 2 @cache_size = 0 @monitor = Monitor.new @pruning = false end |
Instance Method Details
#cleanup(options = nil) ⇒ Object
Preemptively iterates through all stored keys and removes the ones which have expired.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/active_support/cache/memory_store.rb', line 40 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) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/active_support/cache/memory_store.rb', line 31 def clear( = 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.
82 83 84 |
# File 'lib/active_support/cache/memory_store.rb', line 82 def decrement(name, amount = 1, = nil) modify_value(name, -amount, ) end |
#delete_matched(matcher, options = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_support/cache/memory_store.rb', line 86 def delete_matched(matcher, = nil) = () instrument(:delete_matched, matcher.inspect) do matcher = key_matcher(matcher, ) keys = synchronize { @data.keys } keys.each do |key| delete_entry(key, ) if key.match(matcher) end end end |
#increment(name, amount = 1, options = nil) ⇒ Object
Increment an integer value in the cache.
77 78 79 |
# File 'lib/active_support/cache/memory_store.rb', line 77 def increment(name, amount = 1, = nil) modify_value(name, amount, ) end |
#inspect ⇒ Object
:nodoc:
97 98 99 |
# File 'lib/active_support/cache/memory_store.rb', line 97 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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_support/cache/memory_store.rb', line 53 def prune(target_size, max_time = nil) return if pruning? @pruning = true begin start_time = Time.now 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, ) return if @cache_size <= target_size || (max_time && Time.now - start_time > max_time) end end ensure @pruning = false end end |
#pruning? ⇒ Boolean
Returns true if the cache is currently being pruned.
72 73 74 |
# File 'lib/active_support/cache/memory_store.rb', line 72 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.
103 104 105 |
# File 'lib/active_support/cache/memory_store.rb', line 103 def synchronize(&block) # :nodoc: @monitor.synchronize(&block) end |