At the Forge - Memcached Integration in Rails
Now, it's all well and good that we have cached information about each person in memcached. Our database certainly will thank us for that. But, what happens when data about the person changes? The way we've written this application, we're out of luck. Updated information will make its way to the database, but the cache will continue to give us the data it stored long ago. Even if this weren't the case, we still would want to empty the cache on occasion, allowing data to expire if we haven't used it in a while.
To solve the second problem, we can invoke our cache function in a slightly different way, indicating how long we want it to stick around in a second (and optional) argument:
@person = cache(['Person', params[:id]],
:expires_in => 30.minutes) do
Person.find(params[:id])
end
The :expires_in parameter accepts a number of seconds, which we either can enter by hand or via one of the super-convenient Rails extensions to the Fixnum class.
The second problem, one of expiring data manually, requires that we use a less beautiful, but also convenient, way of accessing the cache storage system:
Rails.cache.delete(['controller', 'Person',
↪params[:id]].join('/'))
Basically, we access the cache system using the Rails.cache object and invoke the delete method on it. That method accepts a memcached key. As you might remember, we previously saw that the elements of our key array (as used by the helpful cache method) were joined by slashes and prefixed with controller. Thus, the above works, even though it's not quite as nice as I might have liked. We can see that this is the case in the memcached logs:
<7 delete controller/Person/1 0 >7 DELETED
And, sure enough, we then find that our next invocation of show for person 1 retrieves the information from the database and caches it in memcached.
Caching has long been an excellent way to improve performance in the computer industry, from the hardware level all the way up to operating systems and applications. Rails programmers have incorporated memcached into their applications over the last few years, but I believe that its complete integration in version 2.1 will make it even easier, and more widespread, to find memcached-enabled Rails applications. As you can see, adding just a few lines of configuration and application code can speed up an application by many times, without having to sacrifice accuracy.
Resources
If you are looking for information on memcached, you should begin at www.danga.com/memcached, the home page for the open-source project and the source of a great deal of good documentation, code and general information.
For information on Ruby on Rails, start by going to www.rubyonrails.com, which has pointers to documentation, mailing lists and (of course) software you can download.
For information on the integration of memcached into Rails, try www.thewebfellas.com/blog/2008/6/9/rails-2-1-now-with-better-integrated-caching.
There are some Rails plugins that might make it even easier to cache objects. For example, take a look at www.inwebwetrust.net/post/2008/09/08/query-memcached and lucaguidi.com/pages/cached_models, both of which have gained some attention since Rails 2.1 caching was released.
Finally, a tutorial on the use of memcached with Rails is included in a chapter of Advanced Rails Recipes, published by the Pragmatic Programmers. I have greatly enjoyed this book and recommend it to anyone planning to use Rails for more than a simple application. The chapter on memcached is one that has been released as a free sample, and it is available in PDF as media.pragprog.com/titles/fr_arr/cache_data_easily.pdf.
Reuven M. Lerner, a longtime Web/database developer and consultant, is a PhD candidate in learning sciences at Northwestern University, studying on-line learning communities. He recently returned (with his wife and three children) to their home in Modi'in, Israel, after four years in the Chicago area.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
Web Development News
Developer Poll
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
| Android's Limits | Jun 04, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Introduction to MapReduce with Hadoop on Linux
- Senior Perl Developer
- Technical Support Rep
- Weechat, Irssi's Little Brother
- UX Designer
- One Tail Just Isn't Enough
- Android's Limits
- Free is costly
1 hour 11 min ago - Bought photoshop CS5 for developing a website :(
1 hour 27 min ago - Reply to comment | Linux Journal
2 hours 15 min ago - Reply to comment | Linux Journal
2 hours 15 min ago - Replica Watches
4 hours 40 min ago - Reply to comment | Linux Journal
8 hours 51 min ago - on the path to understanding
8 hours 55 min ago - As a fisher,we know that a
1 day 4 hours ago - All I Say Is Worth Share!
1 day 5 hours ago - GeekSays
1 day 5 hours ago








Comments
Thanks for the write up but
Thanks for the write up but i seem to be having a bit of problem when i refresh my browser. I was expecting to see "undefined class/module Person" as stated but instead i get a different error message "undefined method `cache' for #
Good article.
Thanks for the nice write-up!