[文章作者:张宴 本文版本:v1.4 最后修改:2010.06.11 转载请注明原文链接:http://blog.s135.com/read.php/362/]
Tokyo Cabinet 是日本人 平林幹雄 开发的一款 DBM 数据库,该数据库读写非常快,哈希模式写入100万条数据只需0.643秒,读取100万条数据只需0.773秒,是 Berkeley DB 等 DBM 的几倍。
Tokyo Tyrant 是由同一作者开发的 Tokyo Cabinet 数据库网络接口。它拥有Memcached兼容协议,也可以通过HTTP协议进行数据交换。
Tokyo Tyrant 加上 Tokyo Cabinet,构成了一款支持高并发的分布式持久存储系统,对任何原有Memcached客户端来讲,可以将Tokyo Tyrant看成是一个Memcached,但是,它的数据是可以持久存储的。这一点,跟新浪的Memcachedb性质一样。 相比Memcachedb而言,Tokyo Tyrant具有以下优势:
1、故障转移:Tokyo Tyrant支持双机互为主辅模式,主辅库均可读写,而Memcachedb目前支持类似MySQL主辅库同步的方式实现读写分离,支持“主服务器可读写、辅助服务器只读”模式。
这里使用 $memcache->addServer 而不是 $memcache->connect 去连接 Tokyo Tyrant 服务器,是因为当 Memcache 客户端使用 addServer 服务器池时,是根据“crc32(key) % current_server_num”哈希算法将 key 哈希到不同的服务器的,PHP、C 和 python 的客户端都是如此的算法。Memcache 客户端的 addserver 具有故障转移机制,当 addserver 了2台 Memcached 服务器,而其中1台宕机了,那么 current_server_num 会由原先的2变成1。
引用 memcached 官方网站和 PHP 手册中的两段话:
引用
http://www.danga.com/memcached/
If a host goes down, the API re-maps that dead host's requests onto the servers that are available.
http://cn.php.net/manual/zh/function.Memcache-addServer.php
Failover may occur at any stage in any of the methods, as long as other servers are available the request the user won't notice. Any kind of socket or Memcached server level errors (except out-of-memory) may trigger the failover. Normal client errors such as adding an existing key will not trigger a failover.
3、超大数据量下表现出色:
但是,Tokyo Tyrant 也有缺点:在32位操作系统下,作为 Tokyo Tyrant 后端存储的 Tokyo Cabinet 数据库单个文件不能超过2G,而64位操作系统则不受这一限制。所以,如果使用 Tokyo Tyrant,推荐在64位CPU、操作系统上安装运行。
一、安装
1、首先编译安装tokyocabinet数据库
wget http://www.1978th.net/tokyocabinet/tokyocabinet-1.4.45.tar.gz
tar zxvf tokyocabinet-1.4.45.tar.gz
cd tokyocabinet-1.4.45/
./configure
#注:在32位Linux操作系统上编译Tokyo cabinet,请使用./configure --enable-off64代替./configure,可以使数据库文件突破2GB的限制。
#./configure --enable-off64
make
make install
cd ../
2、然后编译安装tokyotyrant
wget http://www.1978th.net/tokyotyrant/tokyotyrant-1.1.40.tar.gz
tar zxvf tokyotyrant-1.1.40.tar.gz
cd tokyotyrant-1.1.40/
./configure
make
make install
cd ../
curl -X DELETE http://127.0.0.1:11211/key
下面介绍下 tokyotyrant-java RDB
RDB provides simple synchronous API. See the example MRDB
MRDB provides effcient asynchronous API. See the example
package tokyotyrant.example;
import tokyotyrant.MRDB;
import tokyotyrant.networking.NodeAddress;
import tokyotyrant.transcoder.DoubleTranscoder;
import tokyotyrant.transcoder.IntegerTranscoder;
public class MRDBExample {
public static void main(String[] args) throws Exception {
Object value;
// create the object
MRDB db = new MRDB();
// connect to the servers
db.open(NodeAddress.addresses("tcp://localhost:1978"));
// store records
if (!db.await(db.put("foo", "hop"))
|| !db.await(db.put("bar", "step"))
|| !db.await(db.put("baz", "jump"))) {
System.err.println("put error");
}
// retrieve records
value = db.await(db.get("foo"));
if (value != null) {
System.out.println(value);
} else {
System.err.println("get error");
}
// add int
db.put("int", 3, new IntegerTranscoder());
int i = db.await(db.addint("int", 4));
System.out.println(i);
// add double
db.put("d", 3.0D, new DoubleTranscoder());
double d = db.await(db.adddouble("d", 4.0D));
System.out.println(d);
// close the connections
db.close();
}
}
package my.test;
import tokyotyrant.MRDB;
import tokyotyrant.networking.NodeAddress;
/**
* 测试使用MRDB
* @author henry
* @verion 1.0
* @date 2009-7-13
*/
public class TestTTDB {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub