首页 > 编程 > PHP > 正文

php5memcached比php5memcache速度略快一点

2020-03-22 16:37:27
字体:
来源:转载
供稿:网友
  • php5-memcached比php5-memcache速度略快一点

    php5-memcached和php5-memcache是两个php操作memcached的组件,他们是不同人开发的。

    php官网都列出了他们各自的使用方法:

    http://www.php.net/manual/en/book.memcache.php (安装使用:sudo apt-get install php5-memcache)http://www.php.net/manual/en/book.memcached.php (安装使用:sudo apt-get install php5-memcached)

    1. 首先,先安装下apache:

    sudo apt-get update
    sudo apt-get install apache2

    2. 然后安装下php5:

    sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

    3. 接着安装memcached:

    sudo apt-get install memcached

    4. 接着安装php5-memcached和php5-memcache:

    sudo apt-get install php5-memcache

    sudo apt-get install php5-memcached

    5. 最后重启下apache2:

    sudo service apache2 restart

    6.编辑test.php内容如下:

    <?php// Initialize html' target='_blank'>values: 10000 keys of 20 bytes with 40 bytes of data$c = 10000;$values = array();for ($i=0;$i<$c;$i++) $values[sprintf('%020s',$i)]=sha1($i);echo "memcache vs memcached: $c keys";// Memcached$m = new Memcached();$m->addServer('localhost', 11211);$start = microtime(true);foreach ($values as $k => $v) $m->set($k, $v, 3600);$time = microtime(true)-$start;echo "memcached set: $time";$start = microtime(true);foreach ($values as $k => $v) $m->get($k);$time = microtime(true)-$start;echo "memcached get: $time";// Memcache$m = new Memcache();$m->addServer('localhost', 11211);$start = microtime(true);foreach ($values as $k => $v) $m->set($k, $v, 0, 3600);$time = microtime(true)-$start;echo "memcache set: $time";$start = microtime(true);foreach ($values as $k => $v) $m->get($k);$time = microtime(true)-$start;echo "memcache get: $time";?>

    7. 运行http://machinename/test.php 或者 php /var/www/html/test.php

    root@machinename # php /var/www/html/test.php
    memcache vs memcached: 10000 keys
    memcached set: 0.7015380859375
    memcached get: 0.61220598220825
    memcache set: 0.78830289840698
    memcache get: 0.74954390525818

    ~
    root@machinename # php /var/www/html/test2.php
    memcache vs memcached: 10000 keys
    memcache set: 0.78771591186523
    memcache get: 0.75219798088074
    memcached set: 0.69968199729919
    memcached get: 0.60679888725281

    参考文档:

    1.http://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu

    2. http://www.digitalocean.com/community/tutorials/how-to-install-and-use-memcache-on-ubuntu-14-04

    3. http://www.leaseweb.com/labs/2013/03/memcache-vs-memcached-php-benchmark/

    PHP编程

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表