Copy CodeThe code is as follows:
$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). '
';
$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). '
';
$t 1 = microtime_float ();
Apc_store ($mem _sql_key. ' _test ', $arrs, $cache _time);
echo "apc_write=====";
Echo Runtime ($t 1). '
';
$t 1 = microtime_float ();
$mem = new Memcache;
$mem->connect ($memcachehost, $memcacheport);
echo "Mem_c
$t 1 = microtime_float ();
$arrs = $mem->get ($mem _sql_key);
echo "mem_read=====";
Echo Runtime ($t 1). '
';
$t 1 = microtime_float ();
$mem->set ($mem _sql_key. ' _test ', $arrs, 0, $cache _time);
echo "mem_write=====";
Echo Runtime ($t 1). '
';
?>
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_c
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_c
Mem_read=====0.1030ms
Mem_write=====0.0801ms
Of course, the repeated refresh will have different values, here just take a more average value.
Win under the Do not have any reference, mainly look at the results of Linux.
Not connent time, probably read and write the speed of APC is faster than the memcached about one times. Count the Memcache_connect time, which is twice times faster.
APC can implement php file opcode cache, also can implement user cache, is really a good thing.
So, if the site size is still small, all functions can be done on a single server, then the caching scheme should be the preferred APC, regardless of memcached. However, if the scale of the site will continue to expand, this time performance differences can be negligible, you should deploy memcached.
In addition, using memcached across servers is a good idea to use the intranet. Otherwise, the memcached will often be connected over time (more than 100ms), and will be twice times more bandwidth-bound by the impact of routing.
The above describes the memcached of the same server using the cache APC efficiency is higher than memcached demo code, including the memcached aspect of the content, I hope to be interested in PHP tutorial friends helpful.