Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Sugar for a few memcached + ActiveRecord patterns

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 .bundle
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.md
Octocat-spinner-32 example.rb
Octocat-spinner-32 fetch.rb
Octocat-spinner-32 get.rb
Octocat-spinner-32 populate.rb
README.md

HasCache

This is a proof-of-concept.

HasCache wraps up a few memcached-related pattern and then puts some tasty sugar on top:

  • Decouple memcached keys from the methods that know how to populate them
  • Memoizing the result of fetching keys from memcached into a model instance
  • Eager loading of associations

The crux of the biscuit is that you can fetch an object from memcached plus all its related data. Suppose you've got a Post model and you're storing post counts per-author in memcached. Instead of loading that data like so:

author = Author.get(14)
post_count = author.post_count

You can fetch it like so, and use one memcached call instead of two:

author = Author.get(14, :include => [:post_count])

If you're fetching enough ancillary data, wrapping everything into one read might help the amount of time you spend dealing with memcached.

Check out example.rb to see how this looks in your domain model. get.rb shows how to use eager includes.

Plot twist!

In fact, it seems one big read instead of multiple reads is slower. I'm either missing something or writing horribly slow code. Going to look into that sometime soon.


~akk
May 31, 2010
Something went wrong with that request. Please try again.