Home  >  Article  >  php教程  >  PHP APC的安装与使用详解

PHP APC的安装与使用详解

WBOY
WBOYOriginal
2016-06-13 11:49:181218browse

一、PHPAPC安装
下载与解压安装包:

复制代码 代码如下:


wget-c http://pecl.php.net/get/APC-3.1.13.tgz
tar-zvxf APC-3.1.13.tgz
cdAPC-3.1.13


编译安装:

复制代码 代码如下:


wget-c http://pecl.php.net/get/APC-3.1.13.tgz
/usr/local/php/bin/phpize
./configure--enable-apc --enable-mmap --enable-apc-spinlocks--disable-apc-pthreadmutex--with-php-config=/usr/local/php/bin/php-config
make
sudomake install


修改php.ini加入:

复制代码 代码如下:


 extension= "apc.so" ;
;APCsetting
apc.enabled= 1
apc.shm_segments= 1
apc.shm_size= 64M
apc.optimization= 1
apc.num_files_hint= 0
apc.ttl= 0
apc.gc_ttl= 3600
apc.cache_by_default= on


重启apache或者/usr/local/php/sbin/php-fpmrestart

二、PHPAPC配置选项
[APC]
;Alternative PHP Cache用于缓存和优化PHP中间代码
apc.cache_by_default= On
;是否默认对所有文件启用缓冲。
;若设为Off并与以加号开头的apc.filters指令一起用,则文件仅在匹配过滤器时才被缓存。
apc.enable_cli= Off
;是否为CLI版本启用APC功能,仅用于测试和调试目的才打开此指令。
apc.enabled= On
;是否启用APC,如果APC被静态编译进PHP又想禁用它,这是唯一的办法。
apc.file_update_protection= 2
;当你在一个运行中的服务器上修改文件时,你应当执行原子操作。
;也就是先写进一个临时文件,然后将该文件重命名(mv)到最终的名字。
;文本编辑器以及cp,tar 等程序却并不是这样操作的,从而导致有可能缓冲了残缺的文件。
;默认值2表示在访问文件时如果发现修改时间距离访问时间小于2秒则不做缓冲。
;那个不幸的访问者可能得到残缺的内容,但是这种坏影响却不会通过缓存扩大化。
;如果你能确保所有的更新操作都是原子操作,那么可以用0关闭此特性。
;如果你的系统由于大量的IO操作导致更新缓慢,你就需要增大此值。
apc.filters=
;一个以逗号分隔的POSIX扩展正则表达式列表。
;如果源文件名与任意一个模式匹配,则该文件不被缓存。
;注意,用来匹配的文件名是传递给include/require的文件名,而不是绝对路径。
;如果正则表达式的第一个字符是"+"则意味着任何匹配表达式的文件会被缓存,
;如果第一个字符是"-"则任何匹配项都不会被缓存。"-"是默认值,可以省略掉。
apc.ttl= 0
;缓存条目在缓冲区中允许逗留的秒数。0表示永不超时。建议值为7200~36000。
;设为0意味着缓冲区有可能被旧的缓存条目填满,从而导致无法缓存新条目。
apc.user_ttl= 0
;类似于apc.ttl,只是针对每个用户而言,建议值为7200~36000。
;设为0意味着缓冲区有可能被旧的缓存条目填满,从而导致无法缓存新条目。
apc.gc_ttl= 3600
;缓存条目在垃圾回收表中能够存在的秒数。
;此值提供了一个安全措施,即使一个服务器进程在执行缓存的源文件时崩溃,
;而且该源文件已经被修改,为旧版本分配的内存也不会被回收,直到达到此TTL值为止。
;设为零将禁用此特性。
apc.include_once_override= Off
;请保持为Off,否则可能导致意想不到的结果。
apc.max_file_size= 1M
;禁止大于此尺寸的文件被缓存。
apc.mmap_file_mask=
;如果使用–enable-mmap(默认启用)为APC编译了MMAP支持,
;这里的值就是传递给mmap模块的mktemp风格的文件掩码(建议值为"/tmp/apc.XXXXXX")。
;该掩码用于决定内存映射区域是否要被file-backed或者sharedmemory backed。
;对于直接的file-backed内存映射,要设置成"/tmp/apc.XXXXXX"的样子(恰好6个X)。
;要使用POSIX风格的shm_open/mmap就需要设置成"/apc.shm.XXXXXX"的样子。
;你还可以设为"/dev/zero"来为匿名映射的内存使用内核的"/dev/zero"接口。
;不定义此指令则表示强制使用匿名映射。
apc.num_files_hint= 1000
;Web服务器上可能被包含或被请求的不同源文件的大致数量(建议值为1024~4096)。
;如果你不能确定,则设为0;此设定主要用于拥有数千个源文件的站点。
apc.optimization= 0
;优化级别(建议值为0 )。
;正整数值表示启用优化器,值越高则使用越激进的优化。
;更高的值可能有非常有限的速度提升,但目前尚在试验中。
apc.report_autofilter= Off
;是否记录所有由于early/latebinding原因而自动未被缓存的脚本。
apc.shm_segments= 1
;为编译器缓冲区分配的共享内存块数量(建议值为1)。
;如果APC耗尽了共享内存,并且已将apc.shm_size指令设为系统允许的最大值,
;你可以尝试增大此值。
apc.shm_size= 30
;每个共享内存块的大小(以MB为单位,建议值为128~256)。
;有些系统(包括大多数BSD变种)默认的共享内存块大小非常少。
apc.slam_defense= 0
;(反对使用该指令,建议该用apc.write_lock指令)
;在非常繁忙的服务器上,无论是启动服务还是修改文件,
;都可能由于多个进程企图同时缓存一个文件而导致竞争条件。
;这个指令用于设置进程在处理未被缓存的文件时跳过缓存步骤的百分率。
;比如设为75表示在遇到未被缓存的文件时有75%的概率不进行缓存,从而减少碰撞几率。
;鼓励设为0来禁用这个特性。
apc.stat= On
;是否启用脚本更新检查。
;改变这个指令值要非常小心。
;默认值On表示APC在每次请求脚本时都检查脚本是否被更新,
;如果被更新则自动重新编译和缓存编译后的内容。但这样做对性能有不利影响。
;如果设为Off则表示不进行检查,从而使性能得到大幅提高。
;但是为了使更新的内容生效,你必须重启Web服务器。
;这个指令对于include/require的文件同样有效。但是需要注意的是,
;如果你使用的是相对路径,APC就必须在每一次include/require时都进行检查以定位文件。
;而使用绝对路径则可以跳过检查,所以鼓励你使用绝对路径进行include/require操作。
apc.user_entries_hint= 100
;类似于num_files_hint指令,只是针对每个不同用户而言。
;如果你不能确定,则设为0。
apc.write_lock= On
;是否启用写入锁。
;在非常繁忙的服务器上,无论是启动服务还是修改文件,
;都可能由于多个进程企图同时缓存一个文件而导致竞争条件。
;启用该指令可以避免竞争条件的出现。
apc.rfc1867= Off
;打开该指令后,对于每个恰好在file字段之前含有

三、PHP_APC函数
apc_add
—缓存一个变量到数据存储
apc_bin_dump— Get a binary dump of the given files and user variables
apc_bin_dumpfile— Output a binary dump of cached files and user variables to a file
apc_bin_load— Load a binary dump into the APC file/user cache
apc_bin_loadfile— Load a binary dump from a file into the APC file/user cache
apc_cache_info— Retrieves cached information from APC's data store
apc_cas— Updates an old value with a new value
apc_clear_cache—清除APC缓存
apc_compile_file— Stores a file in the bytecode cache, bypassing all filters.
apc_dec— Decrease a stored number
apc_define_constants— Defines a set of constants for retrieval and mass-definition
apc_delete_file— Deletes files from the opcode cache
apc_delete—从用户缓存中删除某个变量
apc_exists—检查APC中是否存在某个或者某些key
apc_fetch—从缓存中取出存储的变量
apc_inc—递增一个储存的数字
apc_load_constants— Loads a set of constants from the cache
apc_sma_info— Retrieves APC's Shared Memory Allocation information
apc_store— Cache a variable in the data store

四、PHPAPC使用
使用apc缓存的示例(test_apc_cache.php):

复制代码 代码如下:


error_reporting(E_ALL);
classapcInit {
/**
*Apc缓存-设置缓存
*设置缓存key,value和缓存时间
*@param string $key KEY值
*@param string $value值
*@param string $time缓存时间
*/
publicstatic function set_cache($key, $value, $time = 0) {
if($time == 0) $time = null; //null情况下永久缓存
returnapc_store($key, $value, $time);;
}
/**
*Apc缓存-获取缓存
*通过KEY获取缓存数据
*@param string $key KEY值
*/
publicstatic function get_cache($key) {
returnapc_fetch($key);
}
/**
*Apc缓存-清除一个缓存
*从memcache中删除一条缓存
*@param string $key KEY值
*/
publicstatic function clear($key) {
returnapc_delete($key);
}
/**
*Apc缓存-清空所有缓存
*不建议使用该功能
*@return
*/
publicstatic function clear_all() {
returnapc_clear_cache(); //清楚缓存
}
/**
*检查APC缓存是否存在
*@param string $key KEY值
*/
publicstatic function exists($key) {
returnapc_exists($key);
}
/**
*字段自增-用于记数
*@param string $key KEY值
*@param int $step新增的step值
*/
publicstatic function inc($key, $step) {
returnapc_inc($key, (int) $step);
}
/**
*字段自减-用于记数
*@param string $key KEY值
*@param int $step新增的step值
*/
publicstatic function dec($key, $step) {
returnapc_dec($key, (int) $step);
}
/**
*缓存文件
*/
publicstatic function cache_file($file) {
returnapc_compile_file($file);
}
/**
*返回APC缓存信息
*/
publicstatic function info() {
returnapc_cache_info();
}
}
apc_clear_cache();
apcInit::cache_file("HessianUtils.php");
apcInit::cache_file("Hessian1Parser.php");
apcInit::cache_file("Hessian1Writer.php");
apcInit::cache_file("HessianStream.php");
apcInit::cache_file("HessianParsing.php");
apcInit::cache_file("HessianExceptions.php");
include_once"./HessianUtils.php";
include_once"./Hessian1Parser.php";
include_once"./Hessian1Writer.php";
include_once"./HessianStream.php";
include_once"./HessianParsing.php";
include_once"./HessianExceptions.php";
header("Content-type:text/html;charset=utf-8");
header("Cache-Control:private,max-age=0,no-cache");
$HessianWriter= new Hessian1Writer();
$array= array(1, 2, 3, array('sff','张三'));
//parser
$resolver= new HessianRuleResolver('./hessian1rules.php');
//解析array
$parser_array= new Hessian1Parser($resolver, newHessianStream($HessianWriter->writeValue($array)));
print_r($parser_array->parse());
?>


五、PHPAPC监控与管理
APC提供了apc.php,用于监控与管理APC缓存。将apc.php放到网站目录中,修改账号、密码,即可通过浏览器监控与管理APC缓存。
apc.php第41、42行:

复制代码 代码如下:


defaults('ADMIN_USERNAME','apc');// Admin Username
defaults('ADMIN_PASSWORD','password'); // Admin Password - CHANGE THIS TO ENABLE!!!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn