willko 发表于 2013-2-4 22:16:52

Tokyo Tyrant 在 php 上不能自动反序列化解决方法

Tokyo Tyrant使用memcached协议连接,但是用php的memcached客户端读取时并没有自动反序列化(unserialize)和解压(gzuncompress)。

一、重写get方法,加上反序列化
缺点:性能不如修改源代码,没解决压缩问题

反序列号比较容易解决,直接对所有值都执行unserialize就可以了,如果解析失败会返回false。
但是解压就麻烦了,我发现值达到一定的长度才会压缩,但是不知道压缩后怎么判断是否压缩了,试着看源码了。看了好久,没找到。http://www.agoit.com/images/smiles/icon_cry.gif 功力有限啊,但是和mmc_request_parse_value这个函数有关。
值得注意的是,当值长度大于memcache.compress_threshold,php memcached客户端会自动压缩,默认是20k

FYI:我解决序列化问题的方法。。。使用压缩会抛出个异常。。
// +----------------------------------------------------------------------+// | Willko Framework                                                   |// +----------------------------------------------------------------------+// | Copyright (c) 2008-2009 Willko Cheng                                 |// +----------------------------------------------------------------------+// | Authors: Willko Cheng <willko@foxmail.com>                           |// +----------------------------------------------------------------------+class lib_TokyoTyrant extends Memcache {public function get($key, $flags = null) {if (isset($flags)) {throw new Exception('Sorry, Do not support compression.');}$result = parent::get($key, $flags);if (is_array($result)) {foreach ($result as $key => $val) {$tmp = unserialize($val);$result[$key] = $tmp === false && $val != 'b:0;' ? $result[$key] : $tmp;}} else {$tmp = unserialize($result);$result = $tmp === false && $result != 'b:0;' ? $result : $tmp;}return $result;}}

2009-2-16新加
压缩和序列化都解决的代码,值得注意的是,要把memcache.compress_threshold的值调高,不然php memcached客户端会帮你自动压缩。
缺点:虽然解决了问题,但是要设置memcache.compress_threshold,这将导致整个memcache客户端都不自动压缩。
解决方法:1.使用ini_set动态设置,2.Tyrant使用memcache客户端,非Tyrant服务器使用memcached客户端。请参见,http://willko.iteye.com/blog/332993
<?php// +----------------------------------------------------------------------+// | Willko Framework                                                   |// +----------------------------------------------------------------------+// | Copyright (c) 2008-2009 Willko Cheng                                 |// | Authors: Willko Cheng <willko@foxmail.com>                           |// +----------------------------------------------------------------------+// | Input Date: 2009-02-14                                             |// | Last Modified: 2009-02-16                                          |// +----------------------------------------------------------------------+// ini_set('memcache.compress_threshold', 2147483647);class lib_TokyoTyrant extends Memcache {public function add($key, $value) {return parent::add($key, gzcompress(serialize($value)));}public function replace($key, $value) {return parent::replace($key, gzcompress(serialize($value)));}public function set($key, $value) {return parent::set($key, gzcompress(serialize($value)));}public function get($key) {$result = parent::get($key);if (is_array($result)) {foreach ($result as $key => $val) {$result[$key] = $val === false ? false : unserialize(gzuncompress($val));}} elseif ($result !== false) {$result = unserialize(gzuncompress($result));}return $result;}}

二、修改c源代码
缺点:版本升级带来困难。并且只解决反序列化的问题,压缩没解决
具体请见:http://syre.blogbus.com/logs/29771714.html


三、使用其它客户端
有个日本人用php写了个客户端,使用socket连接Tyrant,自己封装数据
代码地址:http://openpear.org/repository/Net_TokyoTyrant/trunk
作者介绍:http://blog.livedoor.jp/kistame228/archives/51433029.html

四、PHP Tyrant
这个可以说是目前比较完美的解决方案了,解决压缩、序列化以及支持tt的检索功能
http://mamasam.indefero.net/p/tyrant/

四、pecl客户端
特大好消息,pecl已经有tt的客户端了。http://www.php.net/manual/en/book.tokyo-tyrant.php
页: [1]
查看完整版本: Tokyo Tyrant 在 php 上不能自动反序列化解决方法