Use apc php file to include a large array of speed

Foreword recent rewrite of a system, where there is a need to include a large array of files, almost 1.2M, PHP program ran every piece of code that needs to read the contents from the hard disk, the capacity of the site visit is bound to cause excessive IO. consider using Redis , memcache and other KV store, but worried IO caused by excessive network congestion. It was installed on the local, each will need to install it not too much trouble. So try to think of the array into the machine's memory. EAccelerator and APC second election, after eaccelerator 0.6.5.1 version does not support eaccelerator_put method, simply direct use apc, how to install and how to configure APC I will not explain in detail. example is simple, too much nonsense, that is entered. About APC stands for Alternative PHP cache is a free open source php caching plugin, its goal is to provide a free, open and robust framework for caching and optimizing PHP intermediate code. APC configuration php.ini
[apc]
extension = php_apc.dll
apc.enabled = on 
apc.cache_by_default = off  //一定要off,否则所有php都会被cache
apc.shm_segments = 2
apc.shm_size = 128M // 单位M或者G,网上很多没写,会报错
apc.ttl = 7200
apc.user_ttl = 7200
apc.num_files_hint = 1024
apc.write_lock = On
apc.gc_ttl=3600
apc.ttl=0
apc.max_file_size=2M //最大单个文件大小
APC VS include methods of large files contents of the array: arrFile.php
<?php
 $arr = array(
 "aaaa"=>11,
 "BBBBB"=>11,
 ....忽略几万个
 "CCCCC"=>11
 )
include file snippet
<?php
    include_once 'arrFile.php';
    print_r($arr);
?>
APC accelerated file snippet
<?php
    $arr=apc_fetch('key'); # 读取apc缓存
    $arr=unserialize($arr);
?>
and the apc include all code the following performance comparison
<?php
/**
 * 站点:www.ttlsa.com
 * 作者:凉白开
 * QQ群:3951405 
 */
# include array file
$ti1 = microtime ( true );
include_once 'arrFile.php';
$ti2 = microtime ( true );
$ti3=(($ti2 - $ti1) * 1000);
echo "<B>加载文件数组耗时</B>:".$ti3. 'ms<br>';

# apc save
$arrSeri=serialize($arr);   //将上面include的数组序列化
apc_add("key",$badSeri,300); //序列化后的文件保存到apc缓存中,键为key,过期时间300秒
# 写入一次即可,后续都直接在内存中获取.

# apc fetch
$ta1 = microtime ( true );
$a=apc_fetch('key'); // 从apc中取出内容
$arr=unserialize($a);
$ta2 = microtime ( true );
$ta3=(($ta2 - $ta1) * 1000);
echo "<B>加载apc数组耗时</B>:".$ta3 . 'ms<br>';

# result 对比结果
echo round(($ti3-$ta3)/$ta3*100,2) .'%'; // APC比include快百分之几
?>
Test shots are as follows: [caption id = "attachment_2848" align = "alignnone" width = "526"] and the apc include speed is [/ caption] Finally, code is very simple, easy to apc as memcached used, compared to shorten the time include about twice, the slowest time in the case of double shortened. redis store large next time test how long array of needs. this is not to tell you have to use apc, use apc read and write large array is not necessarily the best method, but this method can be used for probably everyone's work, here's thrown just an idea, hope you can spend please indicate the source:. http: //www.ttlsa.com/html/2847.html

Reproduced in: https: //my.oschina.net/766/blog/211211

Guess you like

Origin blog.csdn.net/weixin_34162401/article/details/91492937