At the Forge - Memcached Integration in Rails
Last month, we talked about memcached, a distributed caching system that is in widespread use among Web sites. The reason for memcached's popularity is its simplicity. With a minimum of overhead and setup, it's possible to set and retrieve nearly any value. Caching values that otherwise would come from the database makes it possible to avoid the database altogether on many occasions, speeding the throughput of a Web application and reducing the load on the database server.
Memcached is a wonderful tool, and it is something nearly every Web developer should have in his or her arsenal to improve site performance. But with the release of Ruby on Rails 2.1, it got even better. Rails now has integrated support for memcached, allowing you to use it almost for free from within your application. There are some caveats and tricks to its use, but once you have those under your belt, you quickly will discover that memcached has improved your site performance dramatically.
This month, we take a look at how to make memcached work inside your Rails applications. We further explore some issues you might encounter when using memcached, some of which are easier to work around than others.
Rails has long come with a multilayered caching system that programmers can tap to speed up applications. You can cache individual pages, controller actions or even page fragments. And indeed, judicious use of the Rails caching commands can result in serious improvements to performance.
But, it was only in version 2.1 that Rails integrated support for caching individual objects. The support for object caching not only has the potential to improve your application's performance dramatically, but it also allows you to work with a variety of different storage facilities, so you can choose the one that's most appropriate for you. Although this article concentrates on the use of memcached, you should know that it's possible to work with not only memcached, but also with caches on the local filesystem, in local memory or even on another Rails-aware server using DRb (distributed Ruby, available as a Ruby gem).
To demonstrate how to use memcached, I'm going to create a simple Rails application, using PostgreSQL as the database:
createdb atf rails --database=postgresql atf
Next, I create a simple object, person, for my application, with the Rails built-in scaffolding that includes a RESTful interface:
./script/generate scaffold person firstname:string ↪lastname:string email_address:string
To import this definition into the database, I run the migration that it created:
Sure enough, if I connect to the database, I can see that the table has been created (Listing 1).
Listing 1. Example Table
atf_development=# \d people Table "public.people" Column | Type | Modifiers --------------+-----------------------------+----------------------------------- id | integer | not null default nextval ↪('people_id_seq'::regclass) firstname | character varying(255) | lastname | character varying(255) | email_address | character varying(255) | created_at | timestamp without time zone | updated_at | timestamp without time zone | Indexes: "people_pkey" PRIMARY KEY, btree (id)
And, if I run the application, I have access (via the RESTful interface) to the various CRUD functions associated with a Person object: Create, Retrieve, Update and Delete. I simply type:
Pick up any e-commerce web or mobile app today, and you’ll be holding a mashup of interconnected applications and services from a variety of different providers. For instance, when you connect to Amazon’s e-commerce app, cookies, tags and pixels that are monitored by solutions like Exact Target, BazaarVoice, Bing, Shopzilla, Liveramp and Google Tag Manager track every action you take. You’re presented with special offers and coupons based on your viewing and buying patterns. If you find something you want for your birthday, a third party manages your wish list, which you can share through multiple social- media outlets or email to a friend. When you select something to buy, you find yourself presented with similar items as kind suggestions. And when you finally check out, you’re offered the ability to pay with promo codes, gifts cards, PayPal or a variety of credit cards.Get the Guide
|Graph Any Data with Cacti!||Apr 27, 2017|
|Be Kind, Buffer!||Apr 26, 2017|
|Preparing Data for Machine Learning||Apr 25, 2017|
|openHAB||Apr 24, 2017|
|Omesh Tickoo and Ravi Iyer's Making Sense of Sensors (Apress)||Apr 21, 2017|
|Low Power Wireless: 6LoWPAN, IEEE802.15.4 and the Raspberry Pi||Apr 20, 2017|
- Graph Any Data with Cacti!
- Teradici's Cloud Access Platform: "Plug & Play" Cloud for the Enterprise
- The Weather Outside Is Frightful (Or Is It?)
- Simple Server Hardening
- Understanding Firewalld in Multi-Zone Configurations
- Preparing Data for Machine Learning
- Gordon H. Williams' Making Things Smart (Maker Media, Inc.)
- Server Technology's HDOT Alt-Phase Switched POPS PDU
- IGEL Universal Desktop Converter