We can significantly increase the performance of our application by caching queries to our database. In Rails, this is known as low-level caching. In this recipe, you will learn how to use Rails.cache.fetch
to cache database queries. You'll see that this technique can be used to cache any expensive operations. Any outside API or network calls also make great candidates for low-level caching. We will be able to use this technique to make significant performance improvements to our application. We'll reduce the load on our database as well as speed up our overall response times.
To complete this recipe, our Rails application needs to have either Redis or memcached set up. For us to test cache in development, we will also need to have it enabled in our development config.
Let's open up config/environments/development.rb
now and ensure that we have the following line set to true
:
config.action_controller.perform_caching = true