Home Backend Development PHP Tutorial Memcached Demonstration code for using cache APC on the same server to be more efficient than Memcached

Memcached Demonstration code for using cache APC on the same server to be more efficient than Memcached

Jul 29, 2016 am 08:41 AM
memcached

复制代码 代码如下:


<?php
$memcachehost = 'localhost';
$memcacheport = '11211';
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function runtime($t1){
return number_format((microtime_float() - $t1)*1000, 4).'ms';
}
$starttime = microtime_float();
$cache_time = '30';
echo "init=====".runtime($starttime).'<br>';
$sql = "SELECT * FROM hx WHERE id = 10006";
$mem_sql_key = md5($sql);
$t1 = microtime_float();
echo "APC_read=====";
$arrs = apc_fetch($mem_sql_key);
echo runtime($t1).'<br>';
$t1 = microtime_float();
apc_store($mem_sql_key.'_test', $arrs, $cache_time);
echo "APC_write=====";
echo runtime($t1).'<br>';
$t1 = microtime_float();
$mem = new Memcache;
$mem->connect($memcachehost, $memcacheport);
echo "MEM_c
$t1 = microtime_float();
$arrs = $mem->get($mem_sql_key);
echo "MEM_read=====";
echo runtime($t1).'<br>';
$t1 = microtime_float();
$mem->set($mem_sql_key.'_test',$arrs,0,$cache_time);
echo "MEM_write=====";
echo runtime($t1).'<br>';
?>


预先把这句SQL的结果在apc和memcached中都缓存了,然后测试读写速度。
在本机windows上结果:
init=====0.0341ms
APC_read=====0.0439ms
APC_write=====0.0920ms
MEM_c
MEM_read=====0.2630ms
MEM_write=====0.2270ms
在服务器上linux上结果:
init=====0.0131ms
APC_read=====0.0520ms
APC_write=====0.0489ms
MEM_c
MEM_read=====0.1030ms
MEM_write=====0.0801ms
当然反复刷新会有不同的值,这里只是取了一个较平均的值。
win下的不具备什么参考性,主要看linux上的结果。
不算connent时间,大概读写的速度apc都比memcached快上一倍左右。算上memcache_connect的时间,也就是快二倍。
APC即可以实现php文件的opcode缓存,也可以实现user cache,实在是个好东西。
所以,如果当网站规模还小的时候,所有功能可以在一台服务器上完成时,那么缓存的方案首选应该就是APC,不用考虑memcached。但如果考虑到网站规模会不断扩大,这点时间的性能差异其实可以忽略不计的,就应该部署memcached了。
另外,跨服务器使用memcached,最好要使用内网。不然的话,受路由的影响,memcached经常会连接超时(超过100ms),而且会凭空多出来两倍的宽带流量。

以上就介绍了Memcached 同台服务器使用缓存APC效率高于Memcached的演示代码,包括了Memcached方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Memcached caching technology optimizes Session processing in PHP Memcached caching technology optimizes Session processing in PHP May 16, 2023 am 08:41 AM

Memcached caching technology optimizes Session processing in PHP

Caching library in PHP8.0: Memcached Caching library in PHP8.0: Memcached May 14, 2023 am 08:16 AM

Caching library in PHP8.0: Memcached

How to optimize PHP application CPU usage using Memcached caching technology? How to optimize PHP application CPU usage using Memcached caching technology? Jun 21, 2023 pm 05:07 PM

How to optimize PHP application CPU usage using Memcached caching technology?

Use Memcached caching technology to optimize audio and video playback in PHP Use Memcached caching technology to optimize audio and video playback in PHP May 17, 2023 pm 04:01 PM

Use Memcached caching technology to optimize audio and video playback in PHP

PHP and Memcached database backup and recovery PHP and Memcached database backup and recovery May 15, 2023 pm 09:12 PM

PHP and Memcached database backup and recovery

Cache management with PHP and Memcached Cache management with PHP and Memcached May 23, 2023 pm 02:21 PM

Cache management with PHP and Memcached

PHP and Memcached performance monitoring PHP and Memcached performance monitoring May 15, 2023 pm 09:51 PM

PHP and Memcached performance monitoring

How to use Memcached technology in PHP? How to use Memcached technology in PHP? May 13, 2023 am 08:21 AM

How to use Memcached technology in PHP?

See all articles