1.安装及配置
在页面 http://pecl.php.net/package/tokyo_tyrant 可以找到php的tt扩展最新稳定版本。
tokyo_tyrant extension provides object oriented API for communicating
with Tokyo Tyrant key-value store.
This extension requires Tokyo Cabinet, Tokyo Tyrant and PHP version 5.2.0+.
php的tt扩展在github.com有一个主页, http://github.com/mkoppanen/php-tokyo_tyrant
debian:/home/software# wget http://pecl.php.net/get/tokyo_tyrant-0.5.0.tgz
--2010-09-07 17:38:32-- http://pecl.php.net/get/tokyo_tyrant-0.5.0.tgz
Resolving pecl.php.net... 76.75.200.106
Connecting to pecl.php.net|76.75.200.106|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26903 (26K) [application/octet-stream]
Saving to: `tokyo_tyrant-0.5.0.tgz'
100%[===============================================>] 26,903 36.5K/s in 0.7s
2010-09-07 17:38:34 (36.5 KB/s) - `tokyo_tyrant-0.5.0.tgz' saved [26903/26903]
debian:/home/software# tar zxvf tokyo_tyrant-0.5.0.tgz
phpize
./configure
make && make install
安装完成以后,在php.ini里面增加一行extension = tokyo_tyrant.so,然后tt扩展就生效了。
debian:/home/software# php -m | grep tokyo
tokyo_tyrant
2.测试
debian:/home/php# vi tt.php
<?php
$tt = new TokyoTyrant("localhost");
$tt->put("language", "C/C++");
echo 'language:' . $tt->get("language") . "/n";
echo 'The count of records is:' . $tt -> num() . "/n";
print_r($tt -> stat());
$it = $tt -> getIterator();
foreach ($it as $key => $val) {
echo "key: $key, val: $val/n";
}
?>
debian:/home/php# telnet localhost 1978
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get language
VALUE language 0 5
C/C++
END
debian:/home/php# php tt.php | more
language:C/C++
The count of records is:498
Array
(
[version] => 1.1.40
[libver] => 323
[protver] => 0.91
[os] => Linux
[time] => 1283855111.219663
[pid] => 1824
[sid] => 0
[type] => hash
[path] => /home/ttserver/database.tch
[rnum] => 498
[size] => 568784
[bigend] => 0
[fd] => 7
[loadavg] => 0.150000
[memsize] => 145842176
[memrss] => 2265088
[ru_user] => 0.596037
[ru_sys] => 30.269891
[ru_real] => 3065.307271
[cnt_put] => 6
[cnt_putkeep] => 0
[cnt_putcat] => 0
[cnt_putshl] => 0
[cnt_putnr] => 0
[cnt_out] => 0
[cnt_get] => 1003
[cnt_mget] => 0
[cnt_vsiz] => 0
[cnt_iterinit] => 4
[cnt_iternext] => 998
[cnt_fwmkeys] => 0
[cnt_addint] => 0
[cnt_adddouble] => 0
[cnt_ext] => 0
[cnt_sync] => 0
[cnt_optimize] => 0
[cnt_vanish] => 0
[cnt_copy] => 0
[cnt_restore] => 0
[cnt_setmst] => 0
[cnt_rnum] => 2
[cnt_size] => 0
[cnt_stat] => 1
[cnt_misc] => 0
[cnt_repl] => 0
[cnt_put_miss] => 0
[cnt_out_miss] => 0
[cnt_get_miss] => 0
)
key: metaData_default_1, val: {"songname":"bb","specialname":"cc"}
key: metaData_default_2, val: {"songname":"bb","specialname":"cc"}
key: metaData_default_3, val: {"songname":"cc","specialname":"cc"}
key: metaData_default_4, val: {"songname":"dd","specialname":""}
key: metaData_default_5, val: {"songname":"ee","specialname":"ee"}
key: top_item_default, val: [{"songid":"1","songname":"bb","specialname":"cc"},{"songid":
"2","songname":"bb","specialname":"cc"},{"songid":"3","songname":"cc","specialname":"cc"}
,{"songid":"4","songname":"dd","specialname":""},{"songid":"5","songname":"ee","specialna
me":"ee"}]
.....
PHP的Tokyo Tyrant扩展还有一个很酷的功能,就是可以遍历TT中的所有数据 。如果用PHP的memcache或memcached扩展来操作TT,是无法遍历TT里面的数据的,这个功能很有用处的,当需要对TT中的所有或部分数据进行批量操作时,这个功能就起作用了,如果你正需要此功能,不妨试一试。 memcached中的数据是可以遍历的 ,通过命令行也可以,通过php程序也可以(前提是要安装php的memcache扩展)。
此扩展还提供TokyoTyrantTable、TokyoTyrantQuery两个类分别用来操作TC的Table Database。
更多的API可以查看 http://cn.php.net/manual/en/book.tokyo-tyrant.php 。