Class: ActiveSupport::Cache::MemoryStore
- Includes:
- Strategy::LocalCache
- Defined in:
- 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 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.
Defined Under Namespace
Modules: DupCoder
Constant Summary
Constants inherited from Store
Store::DEFAULT_POOL_OPTIONS, Store::MAX_KEY_SIZE
Instance Attribute Summary
Attributes inherited from Store
Class Method Summary collapse
-
.supports_cache_versioning? ⇒ Boolean
Advertise cache versioning support.
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
Delete all data stored in a given cache store.
-
#decrement(name, amount = 1, **options) ⇒ Object
Decrement a cached integer value.
-
#delete_matched(matcher, options = nil) ⇒ Object
Deletes cache entries if the cache key matches a given pattern.
-
#increment(name, amount = 1, **options) ⇒ Object
Increment a cached integer value.
-
#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 included from Strategy::LocalCache
#fetch_multi, #local_cache, #middleware, #new_local_cache, #unset_local_cache, #with_local_cache
Methods inherited from Store
#delete, #delete_multi, #exist?, #fetch, #fetch_multi, #mute, #namespace, #namespace=, #new_entry, #read, #read_counter, #read_multi, #silence!, #write, #write_counter, #write_multi
Constructor Details
#initialize(options = nil) ⇒ MemoryStore
Returns a new instance of MemoryStore.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/active_support/cache/memory_store.rb', line 75 def initialize( = nil) ||= {} [:coder] = DupCoder unless .key?(:coder) || .key?(:serializer) # Disable compression by default. [:compress] ||= false super() @data = {} @max_size = [:size] || 32.megabytes @max_prune_time = [:max_prune_time] || 2 @cache_size = 0 @monitor = Monitor.new @pruning = false end |
Class Method Details
.supports_cache_versioning? ⇒ Boolean
Advertise cache versioning support.
90 91 92 |
# File 'lib/active_support/cache/memory_store.rb', line 90 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.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/active_support/cache/memory_store.rb', line 103 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
Delete all data stored in a given cache store.
95 96 97 98 99 100 |
# File 'lib/active_support/cache/memory_store.rb', line 95 def clear( = nil) synchronize do @data.clear @cache_size = 0 end end |
#decrement(name, amount = 1, **options) ⇒ Object
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
168 169 170 171 172 |
# File 'lib/active_support/cache/memory_store.rb', line 168 def decrement(name, amount = 1, **) instrument(:decrement, name, amount: amount) do modify_value(name, -amount, **) end end |
#delete_matched(matcher, options = nil) ⇒ Object
Deletes cache entries if the cache key matches a given pattern.
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/active_support/cache/memory_store.rb', line 175 def delete_matched(matcher, = nil) = () matcher = key_matcher(matcher, ) instrument(:delete_matched, matcher.inspect) do keys = synchronize { @data.keys } keys.each do |key| delete_entry(key, **) if key.match(matcher) end end end |
#increment(name, amount = 1, **options) ⇒ Object
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
151 152 153 154 155 |
# File 'lib/active_support/cache/memory_store.rb', line 151 def increment(name, amount = 1, **) instrument(:increment, name, amount: amount) do modify_value(name, amount, **) end end |
#inspect ⇒ Object
:nodoc:
187 188 189 |
# File 'lib/active_support/cache/memory_store.rb', line 187 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.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_support/cache/memory_store.rb', line 116 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, **) 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 |
#pruning? ⇒ Boolean
Returns true if the cache is currently being pruned.
135 136 137 |
# File 'lib/active_support/cache/memory_store.rb', line 135 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.
193 194 195 |
# File 'lib/active_support/cache/memory_store.rb', line 193 def synchronize(&block) # :nodoc: @monitor.synchronize(&block) end |