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

75 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

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

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 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 Training in Chennai

Datascience Books said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
Data Science Training in Bangalore

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

madhavi reddy said...

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

AI Expert said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
artificial intelligence course in pune

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

Data Science Course in Varanasi - 360DigiTMG said...

Truly mind blowing blog went amazed with the subject they have developed the content. These kind of posts really helpful to gain the knowledge of unknown things which surely triggers to motivate and learn the new innovative contents. Hope you deliver the similar successive contents forthcoming as well.

data science in bangalore

Unknown said...

Thanks for sharing such nice info. I hope you will share more information like this. please keep on sharing!

Python Training In Bangalore | Python Online Training

Artificial Intelligence Training In Bangalore | Artificial Intelligence Online Training

Data Science Training In Bangalore | Data Science Online Training

Machine Learning Training In Bangalore | Machine Learning Online Training

AWS Training In Bangalore | AWS Online Training

IoT Training In Bangalore | IoT Online Training

Adobe Experience Manager (AEM) Training In Bangalore | Adobe Experience Manager (AEM) Online Training

Oracle Apex Training In Bangalore | Oracle Apex Online Training

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

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

trainingcourses 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

InstituteBlr said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
data analytics course in bangalore

trainingcourses said...

What a really awesome post this is. Truly, one of the best posts I've ever witnessed to see in my whole life. Wow, just keep it up.

best data science institute in hyderabad

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

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

trainingcourses said...

I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
best data science institute in hyderabad

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

trainingcourses 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

trainingcourses said...

I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
business analytics course

Data Science Expt 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

Data Scientist said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
Data Science Training in Bangalore

trainingcourses said...

First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.
best data science institute in hyderabad

360DigiTMG-Pune said...

This website and I conceive this internet site is really informative ! Keep on putting up!
data science course in pune

Mallela said...

Thanks for posting the best information and the blog is very important.data science institutes 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 intellingence training in chennai

Maneesha said...

Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on.
data scientist training in hyderabad

Data Science Course in Varanasi - 360DigiTMG said...

Honestly speaking this blog is absolutely amazing in learning the subject that is building up the knowledge of every individual and enlarging to develop the skills which can be applied in to practical one. Finally, thanking the blogger to launch more further too.

data science course in bangalore with placement

DataScienceBOX said...

Fantastic article I ought to say and thanks to the info. Instruction is absolutely a sticky topic. But remains one of the top issues of the time. I love your article and look forward to more.
Data Science Course in Bangalore

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

DataBank said...

Nice to be seeing your site once again, it's been weeks for me. This article which ive been waited for so long. I need this guide to complete my mission inside the school, and it's same issue together along with your essay. Thanks, pleasant share.
Data Science training in Bangalore

DataCharge said...

Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .
Data Science certification training in Bangalore

Mallela said...

Thanks for posting the best information and the blog is very important.digital marketing institute in hyderabad

trainingcourses 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

DataScienceBOX said...

Fantastic article I ought to say and thanks to the info. Instruction is absolutely a sticky topic. But remains one of the top issues of the time. I love your article and look forward to more.
Data Science Course in Bangalore

Mallela said...

Thanks for posting the best information and the blog is very important.artificial intelligence course 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

DataCharge said...

Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .
Data Analytics training in Bangalore

trainingcourses said...

I want to say thanks to you. I have bookmarked your site for future updates.
business analytics course

data scientist course said...

Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on.
data scientist training in hyderabad

DataBank said...

Nice to be seeing your site once again, it's been weeks for me. This article which ive been waited for so long. I need this guide to complete my mission inside the school, and it's same issue together along with your essay. Thanks, pleasant share.
Data Science training 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

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

Data Science Course in Varanasi - 360DigiTMG said...

Tremendous blog quite easy to grasp the subject since the content is very simple to understand. Obviously, this helps the participants to engage themselves in to the subject without much difficulty. Hope you further educate the readers in the same manner and keep sharing the content as always you do.

data science course in faridabad

DataCharge said...

Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .
Data Analytics training in Bangalore

trainingcourses said...

I truly like you're composing style, incredible data, thankyou for posting.
data science training