Module: ActiveRecord::CollectionCacheKey
- Included in:
- Base
- Defined in:
- lib/active_record/collection_cache_key.rb
Instance Method Summary collapse
Instance Method Details
#collection_cache_key(collection = all, timestamp_column = :updated_at) ⇒ Object
:nodoc:
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_record/collection_cache_key.rb', line 3 def collection_cache_key(collection = all, = :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 = collection.max_by(&)._read_attribute() end else column_type = type_for_attribute(.to_s) column = "#{connection.quote_table_name(collection.table_name)}.#{connection.quote_column_name()}" select_values = "COUNT(*) AS #{connection.quote_column_name("size")}, MAX(%s) AS timestamp" if collection.limit_value || collection.offset_value query = collection.spawn query.select_values = [column] subquery_alias = "subquery_for_cache_key" subquery_column = "#{subquery_alias}.#{}" subquery = query.arel.as(subquery_alias) arel = Arel::SelectManager.new(query.engine).project(select_values % subquery_column).from(subquery) else query = collection.unscope(:order) query.select_values = [select_values % column] arel = query.arel end result = connection.select_one(arel, nil, query.bound_attributes) if result.blank? size = 0 = nil else size = result["size"] = column_type.deserialize(result["timestamp"]) end end if "#{key}-#{size}-#{.utc.to_s()}" else "#{key}-#{size}" end end |