모듈: Mongoid::Cacheable

확장자:
ActiveSupport::Concern
포함 항목:
컴포저블
다음에 정의됨:
lib/mongoid/cacheable.rb

개요

캐싱과 관련된 동작을 캡슐화합니다.

인스턴스 메서드 요약 접기

인스턴스 메서드 세부 정보

#cache_keystring

캐시 키를 인쇄합니다. 이렇게 하면 복수형 모델 이름에 다른 값이 추가됩니다.

new_record? - /new Non-nil cache_version을 추가하나요? - /id가 아닌 경우 updated_at를 추가 - /id-updated_at.to_formatted_s(cache_timestamp_format)를 추가합니다. 그렇지 않으면 /id를 추가합니다.

이는 일반적으로 cache() 블록 내에서 호출됩니다.

예시:

캐시 키를 반환합니다.

document.cache_key

반환합니다:

  • (string)

    생성된 캐시 키

[소스 보기]

29
30
31
32
33
34
# 파일 'lib/mongoid/cacheable.rb', 29줄

def cache_key
  반환 "#{모델_키}/new" 만약 new_record?
  반환 "#{model_key}/#{_id}" 만약 cache_version
  반환 "#{모델_}/#{_id}-#{updated_at.utc.to_formatted_s(cache_timestamp_format)}" 만약 try(:updated_at)
  "#{model_key}/#{_id}"
end

#cache_version문자열 | nil

이 모델의 캐시 버전을 반환합니다. 기본값 으로 updated_at 필드 (있는 경우)를 문자열로 반환하고, 모델에 updated_at 필드 없는 경우 nil을 반환합니다. 요구 사항이 다른 모델은 원하는 동작에 맞게 이 메서드를 재정의할 수 있습니다.

TODO: MemoryStore를 사용하여 무언가를 입력한 다음 기록 의 타임스탬프를 업데이트하고 메모리 저장 에서 값을 읽으려고 시도하여 테스트할 수 있습니다. 버전이 변경되었으므로 찾을 수 없습니다.

반환합니다:

  • (string | nil)

    캐시 버전 값

[소스 보기]

47
48
49
50
51
# 파일 'lib/mongoid/cacheable.rb', 47줄

def cache_version
  만약 has_attribute?('updated_at') && updated_at.현재?
    updated_at.utc.to_formatted_s(cache_timestamp_format)
  end
end