Caching of Partial Results

Learn to cache the results of partials.

If everything goes as planned, this page will definitely be a high-traffic area for the site. To respond to requests for this page, we’d need to fetch every product from the database and render each one. We can improve on that. After all, the catalog doesn’t change that often, so there’s no need to start from scratch on each request.

So we can see what we’re doing, we’re first going to modify the configuration for the development environment to turn on caching. To make this easy, Rails provides a handy command to toggle caching on and off in the development environment:

depot> bin/rails dev:cache

Note that this command will cause the server to automatically restart.

Next, we need to make a plan. Upon consideration, we only need to re-render things if a product changes. Even then, we need to render only the products that actually changed. So we need to make two small changes to our template.

First, we mark the sections of our template that we need to update if any product changes and then, inside that section, we mark the subsection that we need in order to update any specific product that changed:

Get hands-on with 1400+ tech skills courses.