umar
umar

Reputation: 4389

How to cache queries in Rails across multiple requests

I want to cache query results so that the same results are fetched "for more than one request" till i invalidate the cache. For instance, I want to render a sidebar which has all the pages of a book, much like the index of a book. As i want to show it on every page of the book, I have to load it on every request. I can cache the rendered sidebar index using action caching, but i also want to actually cache the the query results which are used to generate the html for the sidebar. Does Rails provide a way to do it? How can i do it?

Upvotes: 5

Views: 2338

Answers (2)

mikezter
mikezter

Reputation: 2463

You also could check for your action cache before querying the database in the controller.

Upvotes: 0

John Topley
John Topley

Reputation: 115292

You can cache the query results using ActiveSupport's cache store, which can by backed by a memory store such as memcached, or a database store if you provide your own implementation. Note that you'll want to use a database store if the cache is shared across multiple Ruby processes which will be the case if you're deploying to Mongrel Cluster or Phusion Passenger.

Upvotes: 3

Related Questions