Memcache server with substring deleting and aggregate functions
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Tokyo Cache Cow

Tokyo Cache Cow is MemCache protocol speaking cache server. It offers the ability to delete keys based on a substring as well as aggregate functions based on substrings.


Cache sweepers in rails do not currently operate with memcache because the memcache server itself does not support key matching removal operations. After reading and seeing the performance characteristics of that database, I thought I'd give it a go. Event Machine does the heavy lifting on the network end. Performance is currently comparable to memcached.


You'll need the eventmachine gem installed. As well, you'll have to install Tokyo Cabinet itself (available at and the Tokyo Cabinet Ruby bindings (available at

Example (using the rails client under script/console)

Lets write four keys: other_key, test_key, test_key2 and test_key3.

>> Rails.cache.write('other_key', 'other_value')
=> true
>> Rails.cache.write('test_key', 'test_value')
=> true
>> Rails.cache.write('test_key2', 'test_value2')
=> true
>> Rails.cache.write('test_key3', 'test_value3')
=> true

Read back test_key and make sure life is still good.

=> "test_value2"

But lets delete test_key2 for fun.

>> Rails.cache.delete('test_key2')
=> true

Confirm that test_key2 is really gone.

=> nil

.. but our other keys (namely, test_key) are just fine, thank you.

=> "test_value"

.. lets nuke EVERYTHING with test_key in it though.

>> Rails.cache.delete_matched('test_key')
=> true

Now test_key and test_key3 are both nuked.

=> nil
=> nil

But other_key is still peachy.

=> "other_value"

Aggregate functions

You can invoke aggregates by using the functions avg(), count(), sum(), min() and max().

>> Rails.cache.write('hello1', '1')
=> true
>> Rails.cache.write('hello2', '2')
=> true
>> Rails.cache.write('hello3', '3')
=> true
=> "2.0"
=> "6.0"



>> tokyo_cache_cow --help

Usage: tokyo_cache_cow [options]
    -p, --port[OPTIONAL]             Port (default: 11211)
    -a, --address[OPTIONAL]          Address (default:
    -c, --class[OPTIONAL]            Cache provider class (default: TokyoCacheCow::Cache::TokyoCabinetMemcache)
    -r, --require[OPTIONAL]          require
    -f, --file[OPTIONAL]             File (default: /tmp/tcc-cache)
    -d, --daemonize[OPTIONAL]        Daemonize (default: false)
    -P, --pid[OPTIONAL]              Pid file (default: /tmp/
    -m, --matcher[OPTIONAL]          Special flag for doing matched deletes (not enabled by default)
    -h, --help                       Show this help message.


Any standard memcache client will do, however, there is a special initialize for Rails to enable delete_matched functionality within the built-in memcache client there. To install this into rails:

script/plugin install git://


Right now there is no compression on disk. Things could get big, but Tokyo Cabinet does support various compression schemes, so exposing that to the runner should be trivial. Potentially performance could degrade after time, but I have yet to seriously investigate if thats the case.

Feel the moo.