Sunday, January 23, 2011

Easy data caching in Rails 3

Three common forms of caching

There are 3 types of caching that are very commonly talked about in the rails community; page caching, action caching, and fragment caching. All of these are well documented and are great ways to cache varying amounts of content for your pages.

Is that all the caching I need?

Caching at the view level is great but it isn't necessarily the best fit for every situation. In some cases it may be easier to cache data at a lower level. Perhaps you want to cache the results of an external data request for a given period of time. None of the view caching methods would be a really great fit.

Lower level data caching

Rails 3 actually has a great method for caching data at a lower level than the view. It even uses the same caching store as action and fragment caching. ActiveSupport::Cache::Store allows you to store any serializable Ruby object. Rails even provides an already initialized cache store via Rails.cache.

Example usage and code

I used this on www.checkthesock.com to cache METAR queries. I was using current_metar a gem I built that pulls metar data from ADDS. I made a model called CachedMetar to return cached results to the rest of my application.


Code walk through

This model basically returns the result of the block argument to Rails.cache.fetch the first time it is called. Any time it is called within the next 5 minutes it returns the value that was cached the first time. Everywhere I had previously been calling CurrentMetar::Metar.get_metar("ICAO code") I switched to call CachedMetar.metar("ICAO code") and that was all it took.

Testing

Testing is actually surprisingly easy. I used shoulda and mocha and here is what I ended up with.


Reference

8 comments:

Anonymous said...

Just to make a note: This feature has been documented and available in Rails (specifically, ActiveSupport) since version 2.1.0.

Sally Campbell said...

Thanks for sharing. It is a worthwhile read for any app developer melbourne.

Anonymous said...

Nice information. Thank you for the explanation. It makes caching more understandable to us lay people.

James Daugherty

Nani Babu said...

Nice information. Thank you for the explanation.
cheap web hosting india
digital marketing Brisbane

reginald surict said...

Amazing blog. I also recommend you to have a look at this page to know how to spy whatsapp messages.

sameer khan said...

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old thing going around comes around routine. https://meinehaarklinik.de/

Sri prathana said...

Nice information. Thank you for the explanation.

winter internship for ece students
electrical companies in hyderabad for internship
internship in indore for computer science students
free internship in chennai chennai, tamil nadu
free internship in chennai chennai, tamil nadu
internship for electrical engineering students in bangalore
internship in automobile industry
internship in chennai for mca
free ethical hacking course in chennai
paid internship in pune for computer engineering students

Sri prathana said...

cannot set headers after they are sent to the client
select * into sql server
number pattern program in python using while loop
which of the following numbers must be added to 5678 to give a reminder 35 when divided by 460?
riya sold her car for 50000
flipkart hack mod apk download
c program to print vowels in a string
the given signs signify something
two dimensional array in javascript w3schools
how to hack wifi using ubuntu