Need help with memcached?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

426 Stars 116 Forks Academic Free License v3.0 1.3K Commits 11 Opened issues


A Ruby interface to the libmemcached C client

Services available


Need anything else?

Contributors list


An interface to the libmemcached C client. Build Status


Copyright 2009-2013 Cloudburst, LLC. Licensed under the AFL 3. See the included LICENSE file. Portions copyright 2007-2009 TangentOrg, Brian Aker, licensed under the BSD license, and used with permission.


  • clean API
  • robust access to all memcached features
  • SASL support for the binary protocol
  • multiple hashing modes, including consistent hashing
  • ludicrous speed, including optional pipelined IO with no_reply

The memcached library wraps the pure-C libmemcached client via SWIG.


You need Ruby 1.8.7 or Ruby 1.9.2. Other versions may work, but are not guaranteed. You also need the

libraries, which should be provided through your system's package manager.

Install the gem: sudo gem install memcached --no-rdoc --no-ri


Start a local networked memcached server: $ memcached -p 11211 &

Now, in Ruby, require the library and instantiate a Memcached object at a global level:

require 'memcached'
$cache ="localhost:11211")

Now you can set things and get things:

value = 'hello'
$cache.set 'test', value
$cache.get 'test' #=> "hello"

You can set with an expiration timeout:

value = 'hello'
$cache.set 'test', value, 1
$cache.get 'test' #=> nil

You can get multiple values at once:

value = 'hello'
$cache.set 'test', value
$cache.set 'test2', value
$cache.get ['test', 'test2', 'missing']
  #=> {"test" => "hello", "test2" => "hello"}

You can set a counter and increment it. Note that you must initialize it with an integer, encoded as an unmarshalled ASCII string:

start = 1
$cache.set 'counter', start.to_s, 0, false
$cache.increment 'counter' #=> 2
$cache.increment 'counter' #=> 3
$cache.get('counter', false).to_i #=> 3

You can get some server stats:

$cache.stats #=> {..., :bytes_written=>[62], :version=>["1.2.4"] ...}

Rails 3 and 4

Use memcached_store gem to integrate ActiveSupport cache store and memcached gem


Pipelining updates is extremely effective in memcached, leading to more than 25x write throughput than the default settings. Use the following options to enable it:

:no_block => true,
:buffer_requests => true,
:noreply => true,
:binary_protocol => false

Currently #append, #prepend, #set, and #delete are pipelined. Note that when you perform a read, all pending writes are flushed to the servers.


memcached is threadsafe, but each thread requires its own Memcached instance. Create a global Memcached, and then call Memcached#clone each time you spawn a thread.

thread = do
  cache = $cache.clone
  # Perform operations on cache, not $cache
  cache.set 'example', 1
  cache.get 'example'

Join the thread so that exceptions don't get lost


Legacy applications

There is a compatibility wrapper for legacy applications called Memcached::Rails.


memcached, correctly configured, is at least twice as fast as memcache-client and dalli. See link:BENCHMARKS for details.

Reporting problems

The support forum is here.

Patches and contributions are very welcome. Please note that contributors are required to assign copyright for their additions to Cloudburst, LLC.

Further resources

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.