Sha256: 3c0e432da13e0bddc7edc092624ec24d31ac949ab02fd0129d0abc3cc073ef70

Contents?: true

Size: 991 Bytes

Versions: 22

Compression:

Stored size: 991 Bytes

Contents

# frozen_string_literal: true

module Mongoid

  # Encapsulates behavior around caching.
  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_formatted_s(cache_timestamp_format)
    # Without updated_at - will append /id
    #
    # This is usually called inside a cache() block
    #
    # @example Returns the cache key
    #   document.cache_key
    #
    # @return [ String ] the string with or without updated_at
    def cache_key
      return "#{model_key}/new" if new_record?
      return "#{model_key}/#{_id}-#{updated_at.utc.to_formatted_s(cache_timestamp_format)}" if do_or_do_not(:updated_at)
      "#{model_key}/#{_id}"
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
mongoid-8.0.10 lib/mongoid/cacheable.rb
mongoid-8.1.10 lib/mongoid/cacheable.rb
mongoid-8.1.9 lib/mongoid/cacheable.rb
mongoid-8.0.9 lib/mongoid/cacheable.rb
mongoid-8.1.8 lib/mongoid/cacheable.rb
mongoid-8.1.7 lib/mongoid/cacheable.rb
mongoid-8.1.6 lib/mongoid/cacheable.rb
mongoid-8.0.8 lib/mongoid/cacheable.rb
mongoid-8.1.5 lib/mongoid/cacheable.rb
mongoid-8.1.4 lib/mongoid/cacheable.rb
mongoid-8.0.7 lib/mongoid/cacheable.rb
mongoid-8.1.3 lib/mongoid/cacheable.rb
mongoid-8.1.2 lib/mongoid/cacheable.rb
mongoid-8.0.6 lib/mongoid/cacheable.rb
mongoid-7.5.4 lib/mongoid/cacheable.rb
mongoid-8.1.1 lib/mongoid/cacheable.rb
mongoid-8.0.5 lib/mongoid/cacheable.rb
mongoid-8.1.0 lib/mongoid/cacheable.rb
mongoid-7.5.3 lib/mongoid/cacheable.rb
mongoid-8.0.4 lib/mongoid/cacheable.rb