Sha256: 32ccb1fb4b59ea146e7c825bbceb1ce7224b8d980e1080bb78231687dab71999

Contents?: true

Size: 1.22 KB

Versions: 20

Compression:

Stored size: 1.22 KB

Contents

module ActiveRecord
  module CollectionCacheKey

    def collection_cache_key(collection = all, timestamp_column = :updated_at) # :nodoc:
      query_signature = Digest::MD5.hexdigest(collection.to_sql)
      key = "#{collection.model_name.cache_key}/query-#{query_signature}"

      if collection.loaded?
        size = collection.size
        if size > 0
          timestamp = collection.max_by(&timestamp_column).public_send(timestamp_column)
        end
      else
        column_type = type_for_attribute(timestamp_column.to_s)
        column = "#{connection.quote_table_name(collection.table_name)}.#{connection.quote_column_name(timestamp_column)}"

        query = collection
          .unscope(:select)
          .select("COUNT(*) AS #{connection.quote_column_name("size")}", "MAX(#{column}) AS timestamp")
          .unscope(:order)
        result = connection.select_one(query)

        if result.blank?
          size = 0
          timestamp = nil
        else
          size = result["size"]
          timestamp = column_type.deserialize(result["timestamp"])
        end

      end

      if timestamp
        "#{key}-#{size}-#{timestamp.utc.to_s(cache_timestamp_format)}"
      else
        "#{key}-#{size}"
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/collection_cache_key.rb
activerecord-5.0.2 lib/active_record/collection_cache_key.rb
activerecord-5.0.2.rc1 lib/active_record/collection_cache_key.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/collection_cache_key.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/collection_cache_key.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/collection_cache_key.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/collection_cache_key.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/collection_cache_key.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/collection_cache_key.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/collection_cache_key.rb
abaci-0.3.0 vendor/bundle/gems/activerecord-5.0.0/lib/active_record/collection_cache_key.rb
activerecord-5.0.1 lib/active_record/collection_cache_key.rb
activerecord-5.0.1.rc2 lib/active_record/collection_cache_key.rb
activerecord-5.0.1.rc1 lib/active_record/collection_cache_key.rb
activerecord-5.0.0.1 lib/active_record/collection_cache_key.rb
activerecord-5.0.0 lib/active_record/collection_cache_key.rb
activerecord-5.0.0.rc2 lib/active_record/collection_cache_key.rb
activerecord-5.0.0.racecar1 lib/active_record/collection_cache_key.rb
activerecord-5.0.0.rc1 lib/active_record/collection_cache_key.rb
activerecord-5.0.0.beta4 lib/active_record/collection_cache_key.rb