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

49 comments:

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

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

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

360DigiTMGNoida said...

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

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

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 scientist course said...

Wow what a Great Information about World Day its exceptionally pleasant educational post. a debt of gratitude is in order for the post.
data scientist course

Data Analytics Courses in Bangalore said...

I want to leave a little comment to support and wish you the best of luck.we wish the best best of luck in all your blogging endeavors.
data analytics course in bangalore

360digiTMG Training said...


Very awesome!!! When I searched for this I found this website at the top of all blogs in search engines.

Best Institute for Data Science in Hyderabad

360DigiTMG said...

"Very Nice Blog!!!


Please have a look about "
data scientist malaysia

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

Maneesha said...

This was not just great in fact this was really perfect your talent in writing was great.
data scientist training and placement

traininginstitute said...

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

digital marketing courses in hyderabad with placement

InstituteBlr 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

360DigiTMGAurangabad said...

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me..
machine learning certification in aurangabad

traininginstitute said...

Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
best data science institute in hyderabad

360DigiTMGAurangabad said...

Your content is very unique and understandable useful for the readers keep update more article like this.
data science course aurangabad

DataScienceEXPO said...

Thank you quite much for discussing this type of helpful informative article. Will certainly stored and reevaluate your Website.
Data Science certification Course in Bangalore

traininginstitute said...

Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.
best data science institute in hyderabad

DataScienceEXPO said...

Thank you quite much for discussing this type of helpful informative article. Will certainly stored and reevaluate your Website.
Data Analytics Course in Bangalore

lionelmessi said...

I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.

DevOps Training in Hyderabad

Pallavi reddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
artificial intelligence training in chennai

360DigiTMGAurangabad said...

"Very Nice Blog!!!


Please have a look about "
ai courses aurangabad

Priya Rathod said...

Excellent effort to make this blog more wonderful and attractive.
AWS Training in Hyderabad
AWS Course in Hyderabad

Priya Rathod said...

I'm glad to see the extensive unpretentious component here!.
Data Science Training in Hyderabad
Data Science Course in Hyderabad

traininginstitute said...

Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.
data science course in malaysia

Data Science said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Course in Noida

traininginstitute said...


The blog and data is excellent and informative as well
data science course in malaysia

manasa said...

That is truly great and supportive Information you have given, entirely significant substance. We are likewise into training and you additionally can exploit truly great work arranged courses…

Machine Learning Training in Hyderabad

traininginstitute said...

this is really nice to read..informative post is very good to read..thanks a lot!
data scientist course

ideasontrend said...



Its a really interesting and informative article for me. I appreciate your work and skills. Emily in Paris Clothings

traininginstitute said...

It is extremely nice to see the greatest details presented in an easy and understanding manner.
full stack web development course in malaysia


Ramesh Sampangi said...

Nice blog, thank for sharing with us.
Data Science Training in Hyderabad

data science said...

This post is easy to peruse and acknowledge without forgetting about any subtleties. Extraordinary work!

PMP Training in Malaysia said...

360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

Data Science said...

Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one.
Continue posting. A debt of gratitude is in order for sharing.
data analytics course in warangal

kumar said...

It's late discovering this demonstration. At any rate, it's a thing to be acquainted with that there are such occasions exist. I concur with your Blog and I will have returned to investigate it more later on so please keep up your demonstration.https://360digitmg.com/course/data-analytics-using-python-r

Unknown said...

Amazing knowledge and I like to share this kind of information with my friends and hope they like it they why I do business analytics course in surat

chrispine said...

This post is very simple to read and appreciate without leaving any details out. Great work. Fast X Dante Jacket

chrispine said...

Thanks great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative. Hi! This is very useful post for everyone. Thank you so much this post.Just about everything looks good displayed. Jason Momoa Snakeskin Jacket