I never had a reason to play with memcached and it was on my list of things to learn. Below I will demonstrate an example app very quickly and simply. I’m not doing anything more complicated than simple primitive storing. If you are going to store any objects in memcached, dalli gem will take care of this for you (ymmv).
First let’s create a rails app and an rvm gemset to play in.
rvm use 1.9.2@memcache --create
If you don’t have rails in your global gemset, you can install rails now. I used the 3.1 RC here just to test out 3.1 and memcached at the same time.
gem install rails --pre
gem install dalli
Dalli is written by Mike Perham (awesome guy) and simply wires up Rails.cache to be our memcached server. This is convenient and more standard than creating your own global variable or other configuration.
Next, let’s install memcached if we haven’t already:
brew install memcached
$ memcached -v
You can also start memcached as a service under Mac using the homebrew instructions. I like to leave things in the foreground when I’m first setting them up or grok’ing.
Ok, now we can create our dummy app.
rails new memcache_test
Our dummy rails app is going to have a slow controller action in it. In this case, it could be a row count of a huge database. In your case it could be a slow network call or a result of an expensive SQL operation.
First, edit your Gemfile:
Edit config/environments/development.rb
Rails c should work at this point and this line:
Now generate a controller:
rails g controller posts
Create a route in routes.rb:
Create a simple view in app/views/posts/index.json.erb:
Now when you hit the page it should be slow the first time but fast the second time. After 3 seconds, it’s slow again.
`$ time wget -O - localhost:3000/posts.json HTTP request sent, awaiting response… 200 OK Length: 14 [application/json] real 0m3.049s user 0m0.001s sys 0m0.003s
$ time wget -O - localhost:3000/posts.json HTTP request sent, awaiting response… 200 OK Length: 14 [application/json] real 0m0.049s user 0m0.001s sys 0m0.003s `
So you can see that memcached returned the cached object and avoided the sleep call. In the real world, this would equate to lower page rendering time or better application performance.