跳轉至內容 跳轉至搜尋

Memcached 暫存儲存體

一種在 Memcached 中儲存資料的暫存儲存體實作: memcached.org

這目前是製作網站最熱門的一種暫存儲存體。

特殊功能

  • 叢集並載入平衡。你可以指定多個 Memcached 伺服器,而 MemCacheStore 將會在所有可用伺服器之間載入平衡。如果伺服器發生故障,則 MemCacheStore 將會忽略它,直至其重新啟動為止。

MemCacheStore 實作 Strategy::LocalCache 策略,這在一個區塊內實作一組記憶體內的暫存儲存體。

方法
C
D
I
N
S
W

常數

ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n
 
KEY_MAX_SIZE = 250
 
OVERRIDDEN_OPTIONS = UNIVERSAL_OPTIONS
 

這些選項表示這個實作覆寫的行為,不應該允許傳遞至 Dalli 協力廠商

類別公開方法

new(*addresses)

建立新的 MemCacheStore 物件,並使用給定的 Memcached 伺服器位址。每個位址要不是主機名稱,或是一個像「host_name:port」格式的主機名稱與埠號字串。例如

ActiveSupport::Cache::MemCacheStore.new("localhost", "server-downstairs.localnetwork:8229")

如果未提供任何位址,但 ENV['MEMCACHE_SERVERS'] 已定義,則將改用後者。否則,MemCacheStore 將會連線到 localhost:11211(預設的 Memcached 埠號)。

# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 77
def initialize(*addresses)
  addresses = addresses.flatten
  options = addresses.extract_options!
  if options.key?(:cache_nils)
    options[:skip_nil] = !options.delete(:cache_nils)
  end
  super(options)

  unless [String, Dalli::Client, NilClass].include?(addresses.first.class)
    raise ArgumentError, "First argument must be an empty array, address, or array of addresses."
  end

  @mem_cache_options = options.dup
  # The value "compress: false" prevents duplicate compression within Dalli.
  @mem_cache_options[:compress] = false
  (OVERRIDDEN_OPTIONS - %i(compress)).each { |name| @mem_cache_options.delete(name) }
  @data = self.class.build_mem_cache(*(addresses + [@mem_cache_options]))
end

supports_cache_versioning?()

宣傳暫存版本控制支援。

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

實例公開方法

clear(options = nil)

在所有 Memcached 伺服器上清除整個暫存。這個方法應該在使用共享暫存時謹慎使用。

# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 171
def clear(options = nil)
  rescue_error_with(nil) { @data.with { |c| c.flush_all } }
end

decrement(name, amount = 1, options = nil)

使用 Memcached decr 原子運算符減少快取整數值。傳回更新的值。

如果該金鑰已取消設定或已過期,則它將被設定為 0。Memcached 不支援負計數器。

cache.decrement("foo") # => 0

要設定特定值,請呼叫 write 並傳遞 raw: true

cache.write("baz", 5, raw: true)
cache.decrement("baz") # => 4

遞減非數字值,或寫入沒有raw: true的值,會失敗並傳回 nil

# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 158
def decrement(name, amount = 1, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  instrument(:decrement, key, amount: amount) do
    rescue_error_with nil do
      @data.with { |c| c.decr(key, amount, options[:expires_in], 0) }
    end
  end
end

increment(name, amount = 1, options = nil)

使用 memcached incr 原子操作遞增快取整數值。傳回更新的值。

如果金鑰未設定或已過期,會設定為 amount

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

要設定特定值,請呼叫 write 並傳遞 raw: true

cache.write("baz", 5, raw: true)
cache.increment("baz") # => 6

遞增非數字值,或寫入沒有raw: true的值,會失敗並傳回 nil

# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 132
def increment(name, amount = 1, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  instrument(:increment, key, amount: amount) do
    rescue_error_with nil do
      @data.with { |c| c.incr(key, amount, options[:expires_in], amount) }
    end
  end
end

inspect()

# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 96
def inspect
  instance = @data || @mem_cache_options
  "#<#{self.class} options=#{options.inspect} mem_cache=#{instance.inspect}>"
end

stats()

從 memcached 伺服器取得統計資料。

# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 176
def stats
  @data.with { |c| c.stats }
end

write(name, value, options = nil)

行為類似 ActiveSupport::Cache::Store#write,但支援特定於 memcached 的其他選項。

其他選項

  • raw: true - 將值以原生位元組的形式直接傳送至伺服器。值必須是字串或數字。您僅能對原生值使用 memcached 直接作業,例如 incrementdecrement

  • unless_exist: true - 防止覆寫現有的快取項目。

# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 102