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

40 comments:

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

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

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

    James Daugherty

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

    ReplyDelete
  5. 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/

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

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

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

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

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

    ReplyDelete

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

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

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

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

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

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

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

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

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

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

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

    ReplyDelete


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

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


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

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

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

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

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

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

    ReplyDelete