跳到內容 跳到搜尋

記憶體快取儲存

快取儲存實作,在同一個程序中將所有資料儲存在記憶體中。如果您正在執行多個 Ruby on Rails 伺服器程序(如果您正在使用 Phusion Passenger 或 puma 集群模式,則為這種情況),則表示 Rails 伺服器程序執行個體將無法彼此共用快取資料,並且在這種情況下這可能不是最合適的快取。

此快取有一個由初始化項的設定選項 :size 指定的上限大小(預設為 32Mb)。當快取超過分配的大小時,將執行清理,透過移除最近最少使用的項目將快取裁剪成最大大小的四分之三。

與其他 Cache 儲存實作不同,MemoryStore 預設不壓縮數值。MemoryStore 無法像其他 Store 實作那樣大幅受益於壓縮,因為它不會透過網路傳送資料。但是,當啟用壓縮時,它仍須支付完全的壓縮成本(就 CPU 使用而言)。

MemoryStore 是執行緒安全的。

方法
C
D
I
N
P
S

常數

PER_ENTRY_OVERHEAD = 240
 

類別公開方法

new(options = nil)

# 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?()

宣告支援快取版本化。

# File activesupport/lib/active_support/cache/memory_store.rb, line 88
def self.supports_cache_versioning?
  true
end

執行個體公開方法

cleanup(options = nil)

預先迭代所有已儲存的鍵,並移除過期的鍵。

# 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)

刪除儲存在指定快取儲存中的所有資料。

# 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 = nil)

遞減快取整數值。傳回已更新的數值。

如果鍵未設定或已過期,它將設定為 -amount

cache.decrement("foo") # => -1

若要設定特定值,請呼叫 write

cache.write("baz", 5)
cache.decrement("baz") # => 4
# File activesupport/lib/active_support/cache/memory_store.rb, line 166
def decrement(name, amount = 1, options = nil)
  instrument(:decrement, name, amount: amount) do
    modify_value(name, -amount, options)
  end
end

delete_matched(matcher, options = nil)

如果快取鍵符合指定的模式,請刪除快取記錄。

# 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(名稱, 數量 = 1, options = nil)

遞增快取整數值。傳回更新後的數值。

如果鍵未設定,它將設定為 amount

cache.increment("foo") # => 1
cache.increment("bar", 100) # => 100

若要設定特定值,請呼叫 write

cache.write("baz", 5)
cache.increment("baz") # => 6
# File activesupport/lib/active_support/cache/memory_store.rb, line 149
def increment(name, amount = 1, options = nil)
  instrument(:increment, name, amount: amount) do
    modify_value(name, amount, options)
  end
end

prune(目標大小, 最大時間 = nil)

為了確保記錄符合指定的記憶體透過移除最近最少存取的記錄來整理快取。

# 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?()

如果快取目前正在整理,傳回 true。

# File activesupport/lib/active_support/cache/memory_store.rb, line 133
def pruning?
  @pruning
end