# \[ANN\] memcache-client extensions plugin

**URL:** <https://www.ruby-forum.com/t/ann-memcache-client-extensions-plugin/74718>\
**Category:** Rails\
**Created:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/ann-memcache-client-extensions-plugin/74718 "2006-11-07T10:53:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/ann-memcache-client-extensions-plugin/74718/1 "2006-11-07T10:53:42Z")

</div>

From:  
[http://blog.zvents.com/2006/11/1/rails-plugin-memcacheclient-extensions](http://blog.zvents.com/2006/11/1/rails-plugin-memcacheclient-extensions)

=MemCacheClient Extensions

== About

The memcache-client\_extensions plugins adds three new commands to the  
memcache client API:

1. get\_multi : retrieve more than 1 key in parallel
2. stats : retrieve server performance and utilization statistics
3. flush\_all : empty all information stored in memcached

== Installation

1. This plugin requires that the memcache-client gem is installed.

2. Install the plugin or the gem  
$ script/plugin install  
svn://rubyforge.org/var/svn/zventstools/projects/memcache-client\_extensions

- OR -
# gem install memcache-client\_extensions

== get\_multi

Retrieve multiple values from memcached in parallel, if possible. The  
memcached protocol supports the ability to retrieve multiple keys in a  
single request. Pass in an array of keys to this method and it will:  
a. map the key to the appropriate memcached server  
b. send a single request to each server that has one or more key  
values

Returns a hash of values.

> > CACHE[“a”] = 1  
> > =\> 1  
> > CACHE[“b”] = 2  
> > =\> 2  
> > CACHE.get\_multi([“a”,“b”])  
> > =\> {“a”=\>1, “b”=\>2}

Here’s a benchmark showing the speedup:

CACHE[“a”] = 1  
CACHE[“b”] = 2  
CACHE[“c”] = 3  
CACHE[“d”] = 4  
keys = [“a”,“b”,“c”,“d”,“e”]  
Benchmark.bm do |x|  
x.report { for i in 1…1000; keys.each{|k| CACHE.get(k);} end }  
x.report { for i in 1…1000; CACHE.get\_multi(keys); end }  
end

returns:  
user system total real  
0.180000 0.130000 0.310000 ( 0.459418)  
0.200000 0.030000 0.230000 ( 0.269632)

There’s a fair amount of non-DRY between get\_multi and get (and  
threadsafe\_cache\_get/multi\_threadsafe\_cache\_get and  
cache\_get/multi\_cache\_get for that matter) but I think it’s worth it  
since the extra overhead to handle multiple return values is unneeded  
for a single-key get (which is by far the most common case).

== stats

The stats method returns statistics for each memcached server. An  
explanation of the statistics can be found in the memcached docs:  
[http://cvs.danga.com/browse.cgi/wcmtools/memcached/doc/protocol.txt?rev=HEAD&content-type=text/plain](http://cvs.danga.com/browse.cgi/wcmtools/memcached/doc/protocol.txt?rev=HEAD&content-type=text/plain)

Example:

> > CACHE.stats  
> > =\> {“localhost:11211”=\>{“pid”=\>“20188”, “bytes”=\>“4718”,  
> > “connection\_structures”=\>“4”, “time”=\>“1162278121”,  
> > “pointer\_size”=\>“32”, “limit\_maxbytes”=\>“67108864”, “version”=\>“1.2.0”,  
> > “cmd\_get”=\>“14532”, “cmd\_set”=\>“32”, “bytes\_written”=\>“432583”,  
> > “uptime”=\>“1557”, “curr\_items”=\>“4”, “curr\_connections”=\>“3”,  
> > “total\_connections”=\>“19”, “get\_misses”=\>“0”,  
> > “rusage\_user”=\>“0.119981”, “rusage\_system”=\>“0.313952”,  
> > “total\_items”=\>“32”, “get\_hits”=\>“14532”, “bytes\_read”=\>“190619”}}

== flush\_all

The flush\_all method empties all cache namespaces on all memcached  
servers. This method is very useful for testing your code with  
memcached since you normally want to reset the cache to a known  
(empty) state at the beginning of each test.

== Bugs, Code and Contributing

There’s a RubyForge project set up at:

[http://rubyforge.org/projects/zventstools/](http://rubyforge.org/projects/zventstools/)

Anonymous SVN access:

$ svn checkout svn://rubyforge.org/var/svn/zventstools

Author: Tyler K. (tyler dot kovacs at gmail dot com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/ann-memcache-client-extensions-plugin/74718/2 "2006-11-07T11:00:44Z")

</div>

Cool 🙂

Slightly OT here, but is there a way to access all values from a  
namespace  
in memcached? Like “get all my books” or something. Seems like a good  
extension to your get\_multi function.

But perhaps the memcached protocol doesn’t allow for this. That was my  
finding the last time I read through it. Somebody should fix this  
though:  
clearing/adding-to/retrieving a namespace is a real good feature to add.

Vish

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2006, 11:04am UTC](https://www.ruby-forum.com/t/ann-memcache-client-extensions-plugin/74718/3 "2006-11-07T11:04:45Z")

</div>

No, the memcached protocol doesn’t support a “get all” method. Here’s  
a thread from the memcached mailing that covers the issue:

[http://lists.danga.com/pipermail/memcached/2006-April/002112.html](http://lists.danga.com/pipermail/memcached/2006-April/002112.html)

You could memcache your dataset in an array (in addition to memcaching  
each individual item for single-item retrieval), but that probably  
wouldn’t be a good idea for anything other than very small datasets.
