Image 01 Image 02

Tokyo Tyrant with PHP

Posted on 14th May 2009 by Sameer
8

In 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.



8
Responses to.. Tokyo Tyrant with PHP

1
Ben posted on May 16th 2009

What about the increment/decrement methods?! Are they supported by Tokyo Tyrant/Cabinet?



2
Sameer posted on May 17th 2009

According to the documentation, Tokyo Tyrant supports atomic increment so it should work.



3
Ben posted on May 17th 2009

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



4
Sameer posted on May 17th 2009

Ben, I tested it for you and both increment/decrement and multi-get work.

And, true about the google suggestion. It is something I plan to write more about in the future.



5
Ben posted on May 17th 2009

Great, thanks for testing! Looking forward to reading more about Tyrant. Seems like a very interesting piece of software!



6
dH posted on May 25th 2009

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



7
Sameer posted on June 18th 2009

I’ll give the library a try sometime and maybe post about it. Thanks for letting me know



8
anonymous posted on June 16th 2011

Thanks for the series of informative posts on Tokyo Tyrant !



Leave a reply...