Key-based cache expiration at the collection level
Mar 3, 2015
A common pattern in a Rails app is displaying a list of items:
You can speed up a little by caching the individual posts:
This will cache the HTML in the cache block. When either the HTML or Post model changes, the cache will expire. This is called key-based cache expiration and is pretty sweet.
But with this scheme it’s still required to fetch all posts from the database. What we’d like is a way to also cache the entire structure. To achieve that, we have to add a cache_key to the Post model.