Sha256: c9e2baafba7866a3958ed71ab0ffc827878169dc419400856037d0e1655f8628

Contents?: true

Size: 1.01 KB

Versions: 21

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid

  # Encapsulates behavior around caching.
  #
  # @since 6.0.0
  module Cacheable
    extend ActiveSupport::Concern

    included do
      cattr_accessor :cache_timestamp_format, instance_writer: false
      self.cache_timestamp_format = :nsec
    end

    # Print out the cache key. This will append different values on the
    # plural model name.
    #
    # If new_record?     - will append /new
    # If not             - will append /id-updated_at.to_s(cache_timestamp_format)
    # Without updated_at - will append /id
    #
    # This is usually called insode a cache() block
    #
    # @example Returns the cache key
    #   document.cache_key
    #
    # @return [ String ] the string with or without updated_at
    #
    # @since 2.4.0
    def cache_key
      return "#{model_key}/new" if new_record?
      return "#{model_key}/#{id}-#{updated_at.utc.to_s(cache_timestamp_format)}" if do_or_do_not(:updated_at)
      "#{model_key}/#{id}"
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
mongoid-7.1.11 lib/mongoid/cacheable.rb
mongoid-7.2.6 lib/mongoid/cacheable.rb
mongoid-7.2.5 lib/mongoid/cacheable.rb
mongoid-7.1.10 lib/mongoid/cacheable.rb
mongoid-7.1.9 lib/mongoid/cacheable.rb
mongoid-7.2.4 lib/mongoid/cacheable.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/cacheable.rb
mongoid-7.2.3 lib/mongoid/cacheable.rb
mongoid-7.1.8 lib/mongoid/cacheable.rb
mongoid-7.2.2 lib/mongoid/cacheable.rb
mongoid-7.2.1 lib/mongoid/cacheable.rb
mongoid-7.1.7 lib/mongoid/cacheable.rb
mongoid-7.2.0 lib/mongoid/cacheable.rb
mongoid-7.1.6 lib/mongoid/cacheable.rb
mongoid-7.1.5 lib/mongoid/cacheable.rb
mongoid-7.2.0.rc1 lib/mongoid/cacheable.rb
mongoid-7.1.4 lib/mongoid/cacheable.rb
mongoid-7.1.2 lib/mongoid/cacheable.rb
mongoid-7.1.1 lib/mongoid/cacheable.rb
mongoid-7.1.0 lib/mongoid/cacheable.rb