method

cache_key

Importance_2
v5.1.7 - Show latest stable - 0 notes - Class: ActiveRecord::Relation
cache_key(timestamp_column = :updated_at) public

Returns a cache key that can be used to identify the records fetched by this query. The cache key is built with a fingerprint of the sql query, the number of records matched by the query and a timestamp of the last updated record. When a new record comes to match the query, or any of the existing records is updated or deleted, the cache key changes.

Product.where("name like ?", "%Cosmic Encounter%").cache_key
# => "products/query-1850ab3d302391b85b8693e941286659-1-20150714212553907087000"

If the collection is loaded, the method will iterate through the records to generate the timestamp, otherwise it will trigger one SQL query like:

SELECT COUNT(*), MAX("products"."updated_at") FROM "products" WHERE (name like '%Cosmic Encounter%')

You can also pass a custom timestamp column to fetch the timestamp of the last updated record.

Product.where("name like ?", "%Game%").cache_key(:last_reviewed_at)

You can customize the strategy to generate the key on a per model basis overriding ActiveRecord::Base#collection_cache_key.

Show source
Register or log in to add new notes.