歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> php5-memcached比php5-memcache速度略快一點

php5-memcached比php5-memcache速度略快一點

日期:2017/3/1 12:17:53   编辑:關於Linux

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內容如下:

addServer('localhost', 11211);
$start = microtime(true);
foreach ($values as $k => $v) $m->set($k, $v, 3600);
$time = microtime(true)-$start;
echo "memcached set: $time\n";
$start = microtime(true);
foreach ($values as $k => $v) $m->get($k);
$time = microtime(true)-$start;
echo "memcached get: $time\n";

// 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\n";
$start = microtime(true);
foreach ($values as $k => $v) $m->get($k);
$time = microtime(true)-$start;
echo "memcache get: $time\n";
?>

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.https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu

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

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

Copyright © Linux教程網 All Rights Reserved