跳到內容 跳到搜尋

請參閱 ActiveSupport::Cache::Store 以取得文件。

命名空間
方法
E
L

常數

DEFAULT_COMPRESS_LIMIT = 1.kilobyte
 
DeserializationError = Class.new(StandardError)
 

當快取條目無法反序列化時,由編碼器引發。此錯誤視為快取遺失。

OPTION_ALIASES = { expires_in: [:expire_in, :expired_in] }.freeze
 

將規範選項名稱對應到儲存體會辨識的別名。

UNIVERSAL_OPTIONS = [ :coder, :compress, :compress_threshold, :compressor, :expire_in, :expired_in, :expires_in, :namespace, :race_condition_ttl, :serializer, :skip_nil, ]
 

這些選項對所有快取實作都有意義。個別快取實作可能支援其他選項。

屬性

[RW] format_version

類別公開方法

expand_cache_key(key, namespace = nil)

key 參數擴充成可用於快取儲存體的鍵。選擇性地接受命名空間,所有鍵都會在該命名空間內設定範圍。

如果提供的 key 參數是陣列,或對應到 to_a,則陣列中的每個元素都會轉換成參數/鍵,並串接成單一鍵。例如

ActiveSupport::Cache.expand_cache_key([:foo, :bar])               # => "foo/bar"
ActiveSupport::Cache.expand_cache_key([:foo, :bar], "namespace")  # => "namespace/foo/bar"

key 參數也可以對應到 cache_keyto_param

# File activesupport/lib/active_support/cache.rb, line 117
def expand_cache_key(key, namespace = nil)
  expanded_cache_key = namespace ? +"#{namespace}/" : +""

  if prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
    expanded_cache_key << "#{prefix}/"
  end

  expanded_cache_key << retrieve_cache_key(key)
  expanded_cache_key
end

lookup_store(store = nil, *parameters)

根據提供的選項建立新的 Store 物件。

如果沒有傳遞參數給此方法,則會傳回新的 ActiveSupport::Cache::MemoryStore 物件。

如果您將 Symbol 傳遞為第一個參數,則會在 ActiveSupport::Cache 命名空間下建立對應的快取儲存體類別。例如

ActiveSupport::Cache.lookup_store(:memory_store)
# => returns a new ActiveSupport::Cache::MemoryStore object

ActiveSupport::Cache.lookup_store(:mem_cache_store)
# => returns a new ActiveSupport::Cache::MemCacheStore object

任何其他參數都會傳遞給對應快取儲存體類別的建構函式

ActiveSupport::Cache.lookup_store(:file_store, '/tmp/cache')
# => same as: ActiveSupport::Cache::FileStore.new('/tmp/cache')

如果第一個引數不是 Symbol,那麼它將會直接傳回

ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
# => returns MyOwnCacheStore.new
# File activesupport/lib/active_support/cache.rb, line 85
def lookup_store(store = nil, *parameters)
  case store
  when Symbol
    options = parameters.extract_options!
    # clean this up once Ruby 2.7 support is dropped
    # see https://github.com/rails/rails/pull/41522#discussion_r581186602
    if options.empty?
      retrieve_store_class(store).new(*parameters)
    else
      retrieve_store_class(store).new(*parameters, **options)
    end
  when Array
    lookup_store(*store)
  when nil
    ActiveSupport::Cache::MemoryStore.new
  else
    store
  end
end