quick overview and convenience functions for Tokyo Cabinet

22 Mar
I was using MongoDB, but decided to switch to Tokyo because I wanted something that's embedded, ie not running a separate server. It will make deployment and maintenance easier that way, and if I ever have massive I/O problems, I can switch over to Tokyo Tyrant with the same API. Win for the Happy Path.

On OSX I did this:

(1) port install tokyocabinet

(2) gem install tokyocabinet

(3 Use it like this:

#!/usr/bin/env ruby
require 'rubygems'
require 'tokyocabinet'

def db()
????store = TokyoCabinet::HDB.new
????store.open("countries.tch", TokyoCabinet::HDB::OWRITER | TokyoCabinet::HDB::OCREAT)
????result = yield store
????store.close
????result
end

def fetch(key)
????db { |store| Marshal.load(store.get(key)) }
end

def save(key, val)
????db { |store| store.put(key, Marshal.dump(val)) }
end

save("japan", { "prefectures" => 47 })
fetch("japan") ### returns { "prefectures" => 47 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: