内存缓存存储¶ ↑
一种缓存存储实现,它将所有内容存储在同一进程的内存中。如果您运行多个 Ruby on Rails 服务器进程(如果您使用的是 Phusion Passenger 或 puma 集群模式,情况就是如此),那么这意味着 Rails 服务器进程实例将无法相互共享缓存数据,在这种情况下,这可能不是最合适的缓存。
此缓存的绑定大小由初始化项的 :size 选项指定(默认为 32Mb)。当缓存超出分配的大小时,将发生清理,尝试通过删除最近最少使用的条目来将缓存修剪到最大大小的三分之三。
与 Cache 的其他存储实现不同,MemoryStore 默认不压缩值。MemoryStore 不像 Store 的其他实现那样从压缩中受益,因为它不通过网络发送数据。但是,当启用压缩时,它仍然会因 CPU 使用而承担完整的压缩成本。
MemoryStore 是线程安全的。
- C
- D
- I
- N
- P
- S
常量
| 每个条目的开销 | = | 240 |
类公共方法
new(options = nil) Link
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 73 def initialize(options = nil) options ||= {} options[:coder] = DupCoder unless options.key?(:coder) || options.key?(:serializer) # Disable compression by default. options[:compress] ||= false super(options) @data = {} @max_size = options[:size] || 32.megabytes @max_prune_time = options[:max_prune_time] || 2 @cache_size = 0 @monitor = Monitor.new @pruning = false end
supports_cache_versioning?() Link
声明缓存版本支持。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 88 def self.supports_cache_versioning? true end
实例公共方法
cleanup(options = nil) Link
抢先遍历所有存储的键,并删除已过期的键。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 101 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) Link
删除给定缓存存储中的所有数据。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 93 def clear(options = nil) synchronize do @data.clear @cache_size = 0 end end
decrement(name, amount = 1, **options) Link
递减缓存的整数值。返回更新后的值。
如果键未设置或已过期,则将其设置为 -amount。
cache.decrement("foo") # => -1
要设置特定值,请调用 write
cache.write("baz", 5) cache.decrement("baz") # => 4
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 166 def decrement(name, amount = 1, **options) instrument(:decrement, name, amount: amount) do modify_value(name, -amount, **options) end end
delete_matched(matcher, options = nil) Link
如果缓存键与给定模式匹配,则删除缓存条目。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 173 def delete_matched(matcher, options = nil) options = merged_options(options) matcher = key_matcher(matcher, options) instrument(:delete_matched, matcher.inspect) do keys = synchronize { @data.keys } keys.each do |key| delete_entry(key, **options) if key.match(matcher) end end end
increment(name, amount = 1, **options) Link
递增缓存的整数值。返回更新后的值。
如果键未设置,则将其设置为 amount
cache.increment("foo") # => 1 cache.increment("bar", 100) # => 100
要设置特定值,请调用 write
cache.write("baz", 5) cache.increment("baz") # => 6
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 149 def increment(name, amount = 1, **options) instrument(:increment, name, amount: amount) do modify_value(name, amount, **options) end end
prune(target_size, max_time = nil) Link
为了确保条目适合指定的内存,通过删除最近最少访问的条目来修剪缓存。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 114 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, **options) 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?() Link
如果缓存当前正在被修剪,则返回 true。
Source: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/memory_store.rb, line 133 def pruning? @pruning end