Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (31 sloc) 1.26 KB


Morlock turns your memcached server into a distributed, conflict-eating machine. Rails integration is dug in.


Creating a new Morlock instance


require 'memcache-client'
require 'morlock'

mem_cache_client ="")
morlock =

If you prefer Dalli, use that instead:

require 'dalli'
dc ='localhost:11211')
morlock =


If you're already using MemCacheStore in your Rails app, using Morlock is trivial. Morlock will automatically use the memcached server that is backing Rails.cache.

With Bundler:

gem 'morlock', :require => 'morlock/rails'

Or in any script:

require 'morlock/rails'

At this point, Rails.morlock should be defined and available. Use it instead of morlock in the examples below.

Distributed Locking

Possible usages:

handle_failed_lock unless morlock.lock(key) do
	# We have the lock

morlock.lock(key) { # We have the lock } || raise "Unable to lock!"

morlock.lock(key, :failure => failure_proc) do
	# We have the lock

morlock.lock(key, :failure => failure_proc, :success => success_proc)

Morlock doesn't currently do retries, but this could be added in the future. Feel free to submit a patch!