Andrew Thorp

Cache ActiveRecord models in Memcached with IdentityCache by Shopify

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://'

Then bundle, and tell IdentityCache where Memcached is at:

config.identity_cache_store = :mem_cache_store, => [""])

Usage is even easier, again from the README!

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

# 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.


Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00