Copy Code code as follows:
<?php
$memcachehost = ' localhost ';
$memcacheport = ' 11211 ';
function Microtime_float () {
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}
function Runtime ($t 1) {
Return Number_format (Microtime_float ()-$t 1) *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);
$t 1 = microtime_float ();
echo "apc_read=====";
$arrs = Apc_fetch ($mem _sql_key);
Echo Runtime ($t 1). ' <br> ';
$t 1 = microtime_float ();
Apc_store ($mem _sql_key. _test ', $arrs, $cache _time);
echo "apc_write=====";
Echo Runtime ($t 1). ' <br> ';
$t 1 = microtime_float ();
$mem = new Memcache;
$mem->connect ($memcachehost, $memcacheport);
echo "mem_connet=====". Runtime ($t 1). ' <br> ';
$t 1 = microtime_float ();
$arrs = $mem->get ($mem _sql_key);
echo "mem_read=====";
Echo Runtime ($t 1). ' <br> ';
$t 1 = microtime_float ();
$mem->set ($mem _sql_key. _test ', $arrs, 0, $cache _time);
echo "mem_write=====";
Echo Runtime ($t 1). ' <br> ';
?>
The results of this SQL are cached in both APC and memcached, and then the read-write speed is tested.
Results on native windows:
Init=====0.0341ms
Apc_read=====0.0439ms
Apc_write=====0.0920ms
Mem_connet=====11.0571ms
Mem_read=====0.2630ms
Mem_write=====0.2270ms
Results on Linux on the server:
Init=====0.0131ms
Apc_read=====0.0520ms
Apc_write=====0.0489ms
Mem_connet=====0.0501ms
Mem_read=====0.1030ms
Mem_write=====0.0801ms
Of course, there will be different values for repeated refreshes, and here we have just taken a more average value.
Win under the lack of what reference, mainly to see the results of Linux.
does not calculate connent time, probably reads and writes the speed APC to be about one times faster than memcached. Count the memcache_connect time, that is, twice times faster.
APC can implement php file opcode cache, also can implement user cache, it is a good thing.
Therefore, if the site is small, when all the features can be completed on a server, then the caching scheme preferred should be APC, do not consider memcached. But if you consider the scale of the site will continue to expand, this time the performance difference can be negligible, it should be deployed memcached.
In addition, using memcached across servers, it is best to use intranet. Otherwise, memcached will often connect timeouts (more than 100ms) by routing, and will have twice times more broadband traffic.