Home Backend Development PHP Tutorial Demonstration code that uses cache APC more efficiently than Memcached on the same server_PHP tutorial

Demonstration code that uses cache APC more efficiently than Memcached on the same server_PHP tutorial

Jul 21, 2016 pm 03:40 PM
apc memcached php code use copy efficiency server Demo of cache

复制代码 代码如下:

$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).'
';

$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).'
';

$t1 = microtime_float();
apc_store($mem_sql_key.'_test', $arrs, $cache_time);
echo "APC_write=====";
echo runtime($t1).'
';

$t1 = microtime_float();
$mem = new Memcache;
$mem->connect($memcachehost, $memcacheport);
echo "MEM_connet=====".runtime($t1).'
';

$t1 = microtime_float();
$arrs = $mem->get($mem_sql_key);
echo "MEM_read=====";
echo runtime($t1).'
';
$t1 = microtime_float();
$mem->set($mem_sql_key.'_test',$arrs,0,$cache_time);
echo "MEM_write=====";
echo runtime($t1).'
';
?>

预先把这句SQL的结果在apc和memcached中都缓存了,然后测试读写速度。
在本机windows上结果:
init=====0.0341ms
APC_read=====0.0439ms
APC_write=====0.0920ms
MEM_connet=====11.0571ms
MEM_read=====0.2630ms
MEM_write=====0.2270ms

在服务器上linux上结果:
init=====0.0131ms
APC_read=====0.0520ms
APC_write=====0.0489ms
MEM_connet=====0.0501ms
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),而且会凭空多出来两倍的宽带流量。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/321268.htmlTechArticle复制代码 代码如下: ?php $memcachehost = 'localhost'; $memcacheport = '11211'; function microtime_float(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$use...
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Services CakePHP Services Sep 10, 2024 pm 05:26 PM

This chapter deals with the information about the authentication process available in CakePHP.

See all articles