会动的耳朵  

这几天为了兼容json,将jsond的源码调整了。

但是发现php couchbase在编译的时候依赖的是json的header文件

于是把phpcouchbase 的internal.h的 #include "ext/json/php_json.h" 改为#include "ext/jsond/php_jsond.h"

但是在重启php的时候,报错:

NOTICE: PHP message: PHP Warning:  Cannot load module 'couchbase' because required module 'json' is not loaded in Unknown on line 0

于是把jsond的源码module名称改成json。

 

安装http://pecl.php.net/package/igbinary :替代php serializer

 

安装memcached的时候./configure --prefix=/usr/local/memcached --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/ --enable-memcached-json --enable-memcached-igbinary

也会报出error:checking for session includes... /usr/local/php/include/php checking for json includes... configure: error: Cannot find php_json.h

 

修复方案:1. 修改config.m4 ,  132行:     elif test -f "$phpincludedir/ext/jsond/php_jsond.h"; then

php_memcache.c php_json_encode=>php_jsond_encode, php_json_decode=>php_jsond_decode

38行:改为# include "ext/jsond/php_jsond.h"

 

 最后一步配置:将memcache.ini里面的内容加入到php.ini的最后,并把 memcached.serializer = "igbinary" 改成json rather than jsond

重启php-fpm,测试memcached,set()然后get();get的返回值是object stdClass; [应该返回array]

 

【2014-4-22】换一种方案试试

[这样的要求,只修改php_jsond的源码,其他依赖json包或者方法的扩展不做改动。couchbase, memcached都用到了json的头文件,里面调用了php_json_encode和decode。但是修改的php_jsond的头文件里面有类似兼容的代码。这就冲突了。也就是说 我必须的改memcached的头文件引用的json的头文件为php_jsond的头文件]

case 1.

  php json使用编译安装so(官方已经将json融入到php 源码了),php jsond(已包含json) 编译安装,然后预留json的头文件。

  重新编译php,disable-json;这个时候不会产生php_json.h。然后进入php源码的ext/json,编译json。然后保留php_json.h。php.ini不加载json.so

 

  [将php_jsond.h加上php_json_encode和php_json_decode]

  [ignored]

case 2. 

  php-jsond的php_jsond.h有段代码

    

 41 #ifdef PHP_JSOND_PRIMARY
 42 #define PHP_JSOND_PREFIX json
 43 #define PHP_JSOND_PREFIX_STRING "jsond"
 44 #define PHP_JSOND_CONSTANT "JSON"
 45 #define PHP_JSOND_SERIALIZABLE_INTERFACE JsonSerializable
 46 #define PHP_JSOND_SERIALIZABLE_INTERFACE_STRING "JsonSerializable"
 47 #define PHP_JSOND_NAME(name) php_json_ ## name
 48 #define PHP_JSOND_IDENT(name) json_ ## name
 49 #else

  这些代码如何利用上,把43R  jsond 改成json(这里是扩展名称,用extension_loaded("json")检测);

  再 把jsond的头文件路径直接改成json的头文件路径,扩展名称改成json,这样貌似就一劳永逸,不用改其他扩展的代码了

  更改为

 41 /* compatible with json */
 42 #define PHP_JSOND_PRIMARY 1 【增加】
 43
 44 #ifdef PHP_JSOND_PRIMARY
 45 #define PHP_JSOND_PREFIX json
 46 #define PHP_JSOND_PREFIX_STRING "json"
 47 #define PHP_JSOND_CONSTANT "JSON"
 48 #define PHP_JSOND_SERIALIZABLE_INTERFACE JsonSerializable
 49 #define PHP_JSOND_SERIALIZABLE_INTERFACE_STRING "JsonSerializable"
 50 #define PHP_JSOND_NAME(name) php_json_ ## name
 51 #define PHP_JSOND_IDENT(name) json_ ## name
 52 #else

  php-jsond已经兼容json的预定义常量。不需要做其他改动了

 然后进入php 5.5.6/ext/json,把他作为扩展安装(是为了保留json的头文件)

  测试可以使用json_encode decode 以及其他预定义常量。

  在php.ini将其去掉。

  进入php-jsond,安装如上修改进行编译安装,测试json_encode和decode以及预定义常量通过。【这里需要写test case】

  再一步就是安装memcached和couchbase了。 预祝顺利。

 

 

BTW:优化后再把github源码地址公布。

 

[root@ccx64 php-jsond]# php -f bench/compare_with_json.php | grep "time for"
JSON: time for 100000 iterations: 0.268040
JSOND: time for 100000 iterations: 0.272614
JSON: time for 100000 iterations: 0.155579
JSOND: time for 100000 iterations: 0.157206
JSON: time for 100000 iterations: 0.062961
JSOND: time for 100000 iterations: 0.065687
JSON: time for 100000 iterations: 0.274293
JSOND: time for 100000 iterations: 0.273254
JSON: time for 100000 iterations: 1.820356
JSOND: time for 100000 iterations: 1.823402

 

在升级之前的比对上,json耗费的时间是jsond的2倍多

posted on 2014-04-18 15:53  会动的耳朵  阅读(1927)  评论(0)    收藏  举报