Cache ActiveRecord models in Memcached with IdentityCache by Shopify
Today Shopify open sourced a core piece of our infrastructure: identity cachegithub.com/Shopify/identi…— Tobias Lütke (@tobi) March 7, 2013
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.
Discussion
Sign in or Join to comment or subscribe