Home > Article > Backend Development > Memcached Demonstration code for using cache APC on the same server to be more efficient than Memcached
复制代码 代码如下:
$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_c
$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).'
';
?>
以上就介绍了Memcached 同台服务器使用缓存APC效率高于Memcached的演示代码,包括了Memcached方面的内容,希望对PHP教程有兴趣的朋友有所帮助。