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
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.
Constant Summary
-
PER_ENTRY_OVERHEAD =
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 127240
Class Attribute Summary
Store - Inherited
.instrument | :deprecated: |
.instrument= | :deprecated: |
.logger |
Class Method Summary
Instance Attribute Summary
-
#pruning? ⇒ Boolean
readonly
Returns true if the cache is currently being pruned.
Store - Inherited
#logger, #options, | |
#silence? | Alias for Store#silence. |
Instance Method Summary
-
#cleanup(options = nil)
Preemptively iterates through all stored keys and removes the ones which have expired.
- #clear(options = nil)
-
#decrement(name, amount = 1, options = nil)
Decrement an integer value in the cache.
- #delete_matched(matcher, options = nil)
-
#increment(name, amount = 1, options = nil)
Increment an integer value in the cache.
-
#prune(target_size, max_time = nil)
To ensure entries fit within the specified memory prune the cache by removing the least recently accessed entries.
Store - Inherited
#cleanup | Cleanup the cache by removing expired entries. |
#clear | Clear the entire cache. |
#decrement | Decrement an integer value in the cache. |
#delete | Deletes an entry in the cache. |
#delete_matched | Delete all entries with keys matching the pattern. |
#exist? | Returns |
#fetch | Fetches data from the cache, using the given key. |
#fetch_multi | Fetches data from the cache, using the given keys. |
#increment | Increment an integer value in the cache. |
#mute | Silence the logger within a block. |
#read | Fetches data from the cache, using the given key. |
#read_multi | Read multiple values at once from the cache. |
#silence, | |
#silence! | Silence the logger. |
#write | Writes the value to the cache, with the key. |
Constructor Details
.new(options = nil) ⇒ MemoryStore
# File 'activesupport/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 Attribute Details
#pruning? ⇒ Boolean
(readonly)
Returns true if the cache is currently being pruned.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 72
def pruning? @pruning end
Instance Method Details
#cleanup(options = nil)
Preemptively iterates through all stored keys and removes the ones which have expired.
# File 'activesupport/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)
[ GitHub ]# File 'activesupport/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)
Decrement an integer value in the cache.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 91
def decrement(name, amount = 1, = nil) synchronize do = ( ) if num = read(name, ) num = num.to_i - amount write(name, num, ) num else nil end end end
#delete_matched(matcher, options = nil)
[ GitHub ]# File 'activesupport/lib/active_support/cache/memory_store.rb', line 104
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)
Increment an integer value in the cache.
# File 'activesupport/lib/active_support/cache/memory_store.rb', line 77
def increment(name, amount = 1, = nil) synchronize do = ( ) if num = read(name, ) num = num.to_i + amount write(name, num, ) num else nil end 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.
# File 'activesupport/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