TokyoTyrantããã£ãã使ã£ã¦ã¿ã
via. mixi engineer blog
ãªãã ãä»ããææ¼ãã¾ãããPHPããTokyoTyrantã«ç¹ãã¦ã¿ãã®ã§ã¾ã¨ãã¦ã¿ã¾ããã
ç°å¢
- CentOS5.2 on VMwarePlayer on CF-R4
- PHP5.2.6
- memcache3.0.2β
- TokyoCabinet1.3.11
- TokyoTyrant1.1.4
ã¤ã³ã¹ãã¼ã«
ããã¾ãã®ãã¿ã¼ã³ãã¾ãã¯TokyoCabinetã®ã¤ã³ã¹ãã¼ã«ã
$ wget http://downloads.sourceforge.net/tokyocabinet/tokyocabinet-1.3.11.tar.gz
$ tar zxf tokyocabinet-1.3.11.tar.gz
$ cd ./tokyocabinet-1.3.11/
$ ./configure
$ make
$ sudo make install
$
ç¶ãã¦TokyoTyrantã®ã¤ã³ã¹ãã¼ã«
$ wget http://downloads.sourceforge.net/tokyocabinet/tokyotyrant-1.1.4.tar.gz
$ tar zxf tokyotyrant-1.1.4.tar.gz
$ cd ./tokyotyrant-1.1.4/
$ ./configure
$ make
$ sudo make install
$
èµ·åãã¦ã¿ã
ãã¼ãçªå·ã¯1978ã«ãªãã¾ãã
$ sudo /usr/local/sbin/ttservctl start $ netstat -tnl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:1978 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN tcp 0 0 :::873 :::* LISTEN tcp 0 0 :::80 :::* LISTEN tcp 0 0 :::8081 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 :::443 :::* LISTEN $
ttservctlã®ããã©ã«ãå¤ã¯æ¬¡ã®ããã«ãªã£ã¦ãã¾ãã
basedir="/var/ttserver" port="1978" pidfile="$basedir/pid" logfile="$basedir/log" ulogdir="$basedir/ulog" ulimsiz="256m" sid=1 dbname="$basedir/casket.tch#bnum=1000000" maxcon="65536"
$dbnameã®æ¡å¼µåããtchããªã®ã§ãTCHDB(ãã¡ã¤ã«ãã¼ã¹ã®ããã·ã¥ãã¼ã¿ãã¼ã¹)ã«ãªãããã§ããèµ·åãããã¨ã«$basedirãè¦ãã¦ã¿ãã¨
$ ls /var/ttserver/
casket.tch log pid
$
ã«ãªã£ã¦ããã¯ãã
ã¡ãªã¿ã«ãTCMDBï¼ãªã³ã¡ã¢ãªããã·ã¥ãã¼ã¿ãã¼ã¹ï¼ã§èµ·åããå ´åã¯æ¬¡ã®ãããªæãã
$ sudo /usr/local/bin/ttserver "*"
mixi engineer blogã«ã¯
ã¨ãã£ãã®ã§ãããã©ããããããªï¼ã¨æã£ã¦ã¾ããããã®ã¾ã¾ãã*ããªãã§ããããã
å®éã«ã©ã®åã®ãã¼ã¿ãã¼ã¹ãä½ããããã¯ããã¼ã¿ãã¼ã¹ããªã¼ãã³ããéã«ååã§æ±ºãããã¨ãã§ãã¾ããã*ããªãTCMDBãéãããã»ã»ã»
TokyoTyrantã¨PHP
ã§ãPHPããã®æ¥ç¶æ¹æ³ã§ããã2éãã®æ¹æ³ãããã¾ãã
- memcacheäºæãããã³ã«ã使ã
- REST風ãªHTTPäºæãããã³ã«
PHPï¼memcacheæ¡å¼µï¼TokyoTyrant
ã¾ãã¯ãmemcacheæ¡å¼µã使ã£ããµã³ãã«ãRepcachedのテストã§ä½¿ã£ãã³ã¼ããåã£ããã®ã§ãã
<?php $ttl = 10; $ip = '127.0.0.1'; $port = 1978; $memcache = new Memcache(); if (!@$memcache->pconnect($ip, $port)) { printf('failed to connect %s:%s%s', $ip, $port, PHP_EOL); exit; } $version = $memcache->getVersion(); echo 'server version: ' . $version . PHP_EOL; $get_result = $memcache->get('key99'); echo 'cached data:' . PHP_EOL; var_dump(unserialize($get_result)); if (!$get_result) { for ($i = 0; $i < 100; $i++) { $obj = new stdClass; $obj->str = 'test' . $i; $obj->int = $i; $obj->date = microtime(); if (!$memcache->set('key' . $i, serialize($obj), false, $ttl)) { die ('failed to save data'); } } echo 'saved data (TTL=' . $ttl . ')' . PHP_EOL; }
ã§ããã¹ãå®è¡ãããã°ã©ã å®è¡å¾ã«TokyoTyrantãåèµ·åãããã¼ã¿ãæ®ããã¨ã確èªã
$ tcrmgr vanish localhost $ tcrmgr list localhost $ php tokyotyrant.php server version: 1.1.4 cached data: bool(false) saved data (TTL=10) $ tcrmgr list localhost key0 key1 key2 ï¼ key98 key99 $ php tokyotyrant.php server version: 1.1.4 cached data: object(stdClass)#2 (3) { ["str"]=> string(6) "test99" ["int"]=> int(99) ["date"]=> string(21) "0.81435800 1222933721" } $ sudo /usr/local/sbin/ttservctl stop Stopping the server of Tokyo Tyrant Done $ sudo /usr/local/sbin/ttservctl start Starting the server of Tokyo Tyrant Done $ php tokyotyrant.php server version: 1.1.4 cached data: object(stdClass)#2 (3) { ["str"]=> string(6) "test99" ["int"]=> int(99) ["date"]=> string(21) "0.81435800 1222933721" } $
ã¾ãããã¡ã¤ã«ãã¼ã¹ãªã®ã§ãå½ç¶ã®çµæã¨ããã°å½ç¶ã§ãããã¾ããTCMDBã ã¨åèµ·åããã¨ãã¼ã¿ã¯æ¶ãã¡ããã¾ãããããå½ç¶ã¨ããã°å½ç¶ã§ããã
PHPï¼PEAR::HTTP_Requestï¼TokyoTyrant
REST風ãªHTTPäºæãããã³ã«ã使ã£ããµã³ãã«ã¯æ¬¡ã®ãããªæãã
<?php error_reporting(E_ALL); require_once 'HTTP/Request.php'; define('BASE_URL', 'http://localhost:1978/foo'); // create(or replace) $request = new HTTP_Request(BASE_URL); $request->setMethod('PUT'); $request->setBody('bar'); if (!PEAR::isError($request->sendRequest())) { echo $request->getResponseBody() . PHP_EOL; echo '----' . PHP_EOL; } // get $request = new HTTP_Request(BASE_URL); if (!PEAR::isError($request->sendRequest())) { echo $request->getResponseBody() . PHP_EOL; echo '----' . PHP_EOL; } // delete $request = new HTTP_Request(BASE_URL); $request->setMethod('DELETE'); if (!PEAR::isError($request->sendRequest())) { echo $request->getResponseBody() . PHP_EOL; echo '----' . PHP_EOL; }
å®è¡çµæã¯æ¬¡ã®ã¨ããã
$ tcrmgr vanish localhost $ tcrmgr list localhost $ php tokyotyrant02.php Created ---- bar ---- OK ---- $
ã¾ã¨ã
ã¾ãããã¡ããã¨ç¹ãããã¾ãããã¼ãã§çµãã£ã¡ããããã§ããï½
ã¡ãªã¿ã«ãid:cocoitiããã2008-08-13 - 個々一番のHTTP通信ã§ã»ãã·ã§ã³æ
å ±ãæ ¼ç´ããã¹ãã¬ã¼ã¸ã¨ãã¦ä½¿ãä¾ãåºãã¦ã¾ãã