Rails Illustrated

Rails, Web Design and the User Experience

Getting Started with Tokyo Cabinet

421422 21102890

Tokyo Cabinet is an extremely fast key-value store. It's a lot like memcache: only better.

Advantages over Memcache

  • Tokyo Cabinet can be run as a server (called Tokyo Tyrant) but it can also be used without running as a server.
  • Tokyo Cabinet understands the memcache protocol, but also has its own protocol that has a smaller network footprint.
  • Tokyo Cabinet is more flexible supporting different types of stores.
  • Tokyo Cabinet stores can be easily persisted to disk.

Installation

First, you must install the Tokyo Cabinet libraries:

wget http://tokyocabinet.sourceforge.net/tokyocabinet-1.4.23.tar.gz
tar xvzf tokyocabinet-1.4.23.tar.gz
cd tokyocabinet-1.4.23
./configure
make
sudo make install

Ruby bindings installation

Next install the Ruby interface. Unfortunately, Tokyo Cabinet is not currently bundled as a gem. To install:

wget http://tokyocabinet.sourceforge.net/rubypkg/tokyocabinet-ruby-1.25.tar.gz
tar xvzf tokyocabinet-ruby-1.25.tar.gz
cd tokyocabinet-ruby-1.25/
ruby extconf.rb
make

Installation on Mac OS X Leopard

If, on Mac OS X Leopard, you get an error like this:

ld: warning in /usr/local/lib/libtokyocabinet.dylib, file is not of required architecture

Then replace the instructions above with:

wget http://tokyocabinet.sourceforge.net/rubypkg/tokyocabinet-ruby-1.25.tar.gz
tar xvzf tokyocabinet-ruby-1.25.tar.gz
cd tokyocabinet-ruby-1.25/
ARCHFLAGS="-arch i386" ruby extconf.rb
make clean
make
sudo make install

Now you should have the Ruby interface to the Tokyo Cabinet.

Using Tokyo Cabinet

To use Tokyo Cabinet within Ruby it is as simple as:

require 'tokyocabinet'
include TokyoCabinet

hdb = HDB::new
hdb.open("has-db.tch", HDB::OWRITER | HDB::OCREAT)
hdb.put("this-is-a-key","this-is-a-value")
puts hdb.get("this-is-a-key")

Further Reading

Comments  

1

Hi. There is a Tokyo Cabinet gem by user careo on github: http://github.com/careo/tokyocabinet-ruby/tree/master

I linked some more info here http://blag.ahax.de/post/90023607/exploring-tokyo-cabinet

Andreas Haller wrote on June 8 2009
2

rufus-tokyo [http://github.com/jmettraux/rufus-tokyo/tree/master] is a ruby-ffi-dependent API for TokyoCabinet/Tyrant . Ilya Grigorik [http://www.igvita.com/2009/02/13/tokyo-cabinet-beyond-key-value-store/] wrote a great article!

Vaggelis Typaldos wrote on June 24 2009
3

I try to install the ruby binding in osx it comes up with this message. I had my ruby install from macport. Do you know what that means?

ld warning: duplicate dylib /opt/local/lib/libz.1.dylib

sleeplessboy wrote on September 25 2009

Add Comment

(required)
(required, won't be displayed)

(Use Markdown syntax)