模块:Mongoid::Cacheable

扩展方式:
ActiveSupport::Concern
包含在:
可组合
定义于:
lib/mongoid/cacheable.rb

Overview

封装与缓存相关的行为。

实例方法摘要折叠

实例方法详细信息

# cache_keystring

打印出缓存键。 这将为复数模型名称附加不同的值。

如果 new_record? - 是否会追加 /new 非零的 cache_version? - 追加/ ID非零 updated_at - 追加/ ID-updated_at.to_formatted_s(cache_timestamp_format) 否则 - 追加/ ID

这通常在 cache() 块内调用

例子:

返回缓存键

document.cache_key

返回:

  • ( string )

    生成的缓存键



27
28
29
30
31
32
33
# File 'lib/mongoid/cacheable.rb', line 27

def cache_key
  return " #{ model_key } /new " if new_record?
  return "#{model_key}/#{_id}" if cache_version
  return " #{ model_key } / #{ _id } - #{ updated_at . utc . to_formatted_s ( cache_timestamp_format ) } " if try(:updated_at)

  "#{model_key}/#{_id}"
end

# cache_versionstring | nil

返回此模型的缓存版本。默认下,它返回格式化为字符串的 updated_at字段(如果存在),如果模型没有 updated_at字段,则返回 nil。 具有不同需求的模型可以覆盖此方法以适应其所需的行为。

TODO:我们可以通过使用 MemoryStore 进行测试,在其中放入一些内容,然后更新记录上的时间戳并尝试从内存存储中读取值。它不应该找到它,因为版本已更改。

返回:

  • ( string | nil )

    缓存版本值



46
47
48
49
50
# File 'lib/mongoid/cacheable.rb', line 46

def cache_version
  return 除非 has_attribute?('updated_at') && updated_at.现在?

  updated_at.utc.to_formatted_s(cache_timestamp_format)
end