一、使用 Redis时尽量使用hashget/hashset,少用get/set?

    网上多有评论说hashget/hashset的效率比get/set高,并建议在使用redis时尽量使用hash,少使用get/set的键值。未必啊。这里有一段简单的对比hashset和set的性能比较,各执行1000次的数据插入:

1. 测试代码

$redis = new Redis();

$redis->connect('127.0.0.1', '6379');

$char ='';

//set

$t = microtime(true);

for($i=0;$i<1000;$i++)

{

$key.= rand().rand();

$redis->set($key,$key);

}

$cost = microtime(true)-$t;

echo $cost.'
';

//hset

$t = microtime(true);

for($i=0;$i<1000;$i++)

{

$key.= rand().rand();

$redis->hset('hashval', $key, $key);

}

$cost = microtime(true)-$t;

echo $cost.'
';

2. 测试结果

     如下 为单次的测试结果。 

0.70904016494751 1.2040688991547

    单次不具有代表性,进行了多次测试,均显示hashset的用时均比set高。再进行取数据测试,仍是取1000次数据,代码如下:

$redis = new Redis();

$redis->connect('127.0.0.1', '6379');

$redis->set('test','test');

$redis->set('testhash','testhash','testhash');

//get

$t = microtime(true);

for($i=0;$i<1000;$i++)

{

$redis->get('test');

}

$cost = microtime(true)-$t;

echo $cost.'
';

//hget

$t = microtime(true);

for($i=0;$i<1000;$i++)

{

$redis->hget('testhash', 'testhash');

}

$cost = microtime(true)-$t;

echo $cost.'
';

随机单次测试结果:

0.12400698661804 0.09700608253479

3.测试结论

        可见,在进行取数据时,hashget比get要快,综合来说读数据时hash要快,存数据时hash要慢。所以在实际使用时要考虑业务中是读多还是读少了。

    如果像缓存那样设置了一次后,很久才会更新一次,就建议使用hash。     如果数据更新非常频繁,可考虑使用set/get。

publish:March 22, 2016 -Tuesday

二、PHP Warning: PHP Startup: redis: Unable to initialize module 

    redis已成功安装,且已经在php中安装了redis.so,但升级php版本后不能使用原来的redis.so扩展,需要重新编译安装phpredis。今天安装后重启php-fpm时出现了以下错误:

[root@kermit etc]# php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm [21-Jul-2016 18:59:16] NOTICE: PHP message: PHP Warning:  PHP Startup: redis: Unable to initialize module Module compiled with module API=20060613 PHP    compiled with module API=20090626 These options need to match  in Unknown on line 0  done

    出现这个问题的原因是:执行编译源码包执行phpize的版本与当前php环境中phpize的版本不一致造成的。在这之前我非常清楚需要重新编译安装phpredis,不过我是在原来编译安装phpredis的文件夹里继续编译安装的。所以我进入目录以后执行:

/usr/local/php/bin/phpize #重新使用新phpize生成configure配置文件 ./configure --with-php-config=/usr/local/php/bin/php-config  #配置 make && make install  #安装

    但这样依然是不行,说明被编译安装过的文件夹里有一些东西重新编译后不会变化。必须重新编译;即重新解压缩源码包,进入文件夹执行以上安装步骤。不过并不需要更新phpredis版本。

推荐文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: