Same server using cache APC more efficient than memcached demo code _php tips

Source: Internet
Author: User
Tags apc memcached php file
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.