key-value snippets

How to install and use Redis with Ruby

Tagged redis, database, cat, ruby, key-value  Languages ruby

Redis and redis-rb example:

git clone git://github.com/ezmobius/redis-rb.git
cd redis-rb/
rake redis:install
rake dtach:install
rake redis:start &
rake install

cat <<EOF >>test.rb
require 'rubygems'
require 'redis'

r = Redis.new

r.delete('foo')

p'set foo to "bar"'
r['foo'] = 'bar'

p 'value of foo'
p r['foo']
EOF

ruby test.rb

If Redis is not started Ruby will exit at Redis.new for some reason without saying anything.

How to install Tokyo Tyrant, Tokyo Cabinet and rufus-tokyo

Tagged tokyo-cabinet, key-value, database, tokyo-tyrant, ruby  Languages ruby
git clone git://github.com/etrepum/tokyo-cabinet.git
git clone git://github.com/etrepum/tokyo-tyrant.git


cd tokyo-cabinet/
./configure
make
sudo make install

cd ..

cd tokyo-tyrant/
./configure
make
sudo make install

ttserver -port 45001 data.tch

sudo gem install rufus-tokyo

cat <<EOF >>test.rb

require 'rubygems'
require 'rufus/tokyo/tyrant'
require 'benchmark'


#db = Rufus::Tokyo::Cabinet.new('data.tch')

db = Rufus::Tokyo::Tyrant.new('localhost', 45001)
 
 seconds = Benchmark.realtime do
  for i in 1..1000000
    db['nada' + i.to_s] = 'surf' + i.to_s
  end
end

puts seconds
db.close

EOF

ruby test.rb

Gotchas

One gotcha I learned was that you should only use strings not symbols. If you use symbols you'll get this weird error:

Invalid String value

Another gotcha is that Tokyo Cabinet will lock a database. If you try to open the same database from another process it will wait for the other process to close the database.

Debian

On Debian you need to install bzlib before installing tokyo-cabinet:

apt-get install libbz2-dev