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 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 is actually surprisingly easy. I used shoulda and mocha and here is what I ended up with.



Anonymous said...

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

Unknown 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

Unknown 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.

Unknown 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.

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

norhan said...

تنظيف منازل بالدمام شركة تنظيف
تنظيف منازل بالاحساء شركة تنظيف منازل بالاحساء
تنظيف منازل بمكة شركة تنظيف منازل بمكة
تنظيف منازل بجدة شركة تنظيف منازل بجدة
تنظيف منازل بالمدينة المنورة شركة تنظيف المنازل بالمدينة المنورة

subha said...

Some truly wonderful work on behalf of the owner of this internet site , perfectly great articles thansk a lot
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

Priya said...

Great Post
Data Science Institutes In Hyderabad

EXCELR said...

Informative post. Glad to find yo ur blog. Thanks for science course in Hyderabad

Excelr Tuhin said...

I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!
data science course in Hyderabad

360DigiTMG said...

This is a great motivational article. In fact, I am happy with your good work. They publish very supportive data, really. Continue. Continue blogging. Hope you explore your next post
data scientist certification

Maneesha said...

I needed to leave a little remark to help you and wish you a decent continuation. Wishing you good luck for all your contributing to a blog endeavors.
data scientist training

360DigiTMGNoida said...

As always your articles do inspire me. Every single detail you have posted was great.
data science certification in noida

360DigiTMG said...

This post is very simple to read and appreciate without leaving any details out. Great work!
data science course

Datascience Course Analyst said...

Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
Data Science Course in Bangalore

Unknown said...

Wow, amazing post! Really engaging, thank you.
best data analytics training in yelahanka

360digiTMG Training said...

I want to say thanks to you. I have bookmarked your site for future updates.
Best Data Science Courses in Hyderabad

Mallela said...

Thanks for posting the best information and the blog is very informative.Data science course in Faridabad

AI course in pune said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
artificial intelligence course in pune

data analytics books said...

I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
data analytics training in bangalore

Data Scientist said...

Fantastic blog extremely good well enjoyed with the incredible informative content which surely activates the learners to gain the enough knowledge. Which in turn makes the readers to explore themselves and involve deeply in to the subject. Wish you to dispatch the similar content successively in future as well.

Data Science Course in Raipur