As the quote says, Shopify took a step to open source their solution for caching ActiveRecord model objects in Memcached. An elegant solution backed by a powerful data store, IdentityCache has already been used in production. It’s fairly simple to install, straight from the README:

Add this to your Gemfile:

gem 'identity_cache', :git => 'git://github.com/Shopify/identity_cache.git'

Then bundle, and tell IdentityCache where Memcached is at:

config.identity_cache_store = :mem_cache_store, Memcached::Rails.new(:servers => ["mem1.server.com"])

Usage is even easier, again from the README!

class Product < ActiveRecord::Base
  include IdentityCache
  has_many :images
  cache_has_many :images, :embed => true
end

# Fetch the product by it's id, the primary index.
@product = Product.fetch(id)

# Fetch the images for the Product. 
# Images are embedded so the product fetch would have 
# already loaded them.
@images = @product.fetch_images

IdentityCache also has support for: Secondary Indexes, Caching Associations and more. Head on over to GitHub to View the README or the Source Code.


Have comments? Send a tweet to @TheChangelog on Twitter.

Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.