Skip to content

Instantly share code, notes, and snippets.

@rust
Created January 9, 2009 02:38
Show Gist options
  • Save rust/45000 to your computer and use it in GitHub Desktop.
Save rust/45000 to your computer and use it in GitHub Desktop.
require 'tokyotyrant'
include TokyoTyrant
# create the object
rdb = RDB::new
# connect to the server
if !rdb.open("localhost", 1978)
ecode = rdb.ecode
STDERR.printf("open error: %s\n", rdb.errmsg(ecode))
end
# store records
if !rdb.put("foo", "hop") ||
!rdb.put("bar", "step") ||
!rdb.put("baz", "jump")
ecode = rdb.ecode
STDERR.printf("put error: %s\n", rdb.errmsg(ecode))
end
# retrieve records
value = rdb.get("foo")
if value
printf("%s\n", value)
else
ecode = rdb.ecode
STDERR.printf("get error: %s\n", rdb.errmsg(ecode))
end
# traverse records
rdb.iterinit
while key = rdb.iternext
value = rdb.get(key)
if value
printf("%s:%s\n", key, value)
end
end
# hash-like usage
rdb["quux"] = "touchdown"
printf("%s\n", rdb["quux"])
rdb.each do |key, value|
printf("%s:%s\n", key, value)
end
# close the connection
if !rdb.close
ecode = rdb.ecode
STDERR.printf("close error: %s\n", rdb.errmsg(ecode))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment