使用 Redis时尽量使用hashget/hashset,少用get/set? 以及PHP Warning:PHP Startup:redis:Unable to initialize module

一、使用 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.'<br>';

//hset
$t  = microtime(true);
for($i=0;$i<1000;$i++)
{
    $key.= rand().rand();
    $redis->hset('hashval', $key, $key);
}
$cost = microtime(true)-$t;
echo $cost.'<br>';

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.'<br>';

//hget
$t  = microtime(true);
for($i=0;$i<1000;$i++)
{
    $redis->hget('testhash', 'testhash');
}
$cost = microtime(true)-$t;
echo $cost.'<br>';

随机单次测试结果:

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版本。

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林戈的IT生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值