Tokyo Tyrant with PHP
Posted on 14th May 2009 by SameerIn previous posts I’ve introduced Tokyo Cabinet and shown how to install both Tokyo Cabinet and Tokyo Tyrant. In this post, I will show how simple it is to interact with Tokyo Cabinet using PHP.
To summarize, Tokyo Cabinet is an exceptionally fast key/value store. Tokyo Tyrant sits on top of Tokyo Cabinet exposing access to the underlying data via memcache and http networking proctocols.
First begin by creating your database (we will use a hash) and then starting the Tyrant server on 127.0.0.1:80351.
tchmgr create db.tch ttserver -dmn -host 127.0.0.1 -port 80351 db.tch
And because, Tokyo Tyrant implements the memcache protocol, we can use PHP’s existing support for memcache to interact with our database:
<?php
// connect to tokyo tyrant via memcache protocol
$memcache_obj = new Memcache;
$memcache_obj->connect('127.0.0.1', 80351);
// set value
$memcache_obj->set('key','Sameer Parwani');
// get
$get = $memcache_obj->get('key');
// it works!
echo "My name is $get"
The above outputs
My name is Sameer Parwani
Keep in mind that Tokyo Tyrant will not support memcache’s automatic expiration option.
What about the increment/decrement methods?! Are they supported by Tokyo Tyrant/Cabinet?
I once looked at Tokyo Tyrant and the only thing I could find about (atomic) increment/decrement was built using some sort of Lua extension. I never tried it because the PHP support is limited (no php library/api, only the memcache protocol). So if you could test it, that would be awesome!
(and what about multiget..)
(and a small suggestion if I may, there’s hardly any tokyo tyrant + php info on the web, you would get a lot of visitors if you’ld write more about the subject (you’re already result #7 on google ‘tokyo tyrant php’
))
Thanks
Great, thanks for testing! Looking forward to reading more about Tyrant. Seems like a very interesting piece of software!
Hey,
I made a quick native PHP interface for Tokyo Tyrant. It’s still under development, but basic functions are available now: http://code.google.com/p/phptyrant
Thanks for the series of informative posts on Tokyo Tyrant !

