cache_key
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (-38)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
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.