🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## PHP四大加速缓存器opcache,apc,xcache,eAccelerator eAccelerator,xcache,**opcache**(5.5默认 linux下安装php时记得加上 --enable-opcache ),apc(偏数据库缓存,分系统和用户缓存,不推荐5.5将弃之,由opcache替代,apcu作为数据缓存独立出来,在单台服务器时性能优于memcachedd的,多台不推荐)是PHP缓存扩展, memcached缓存是数据库缓存扩展(APC比较偏向数据库缓存的),一般两者只有要安装其中一个即可 注意:([详情连接](https://www.chenweiliang.com/cwl-940.html)) 实际测试中发现当Cache数量超过一定阀值的时候,APC的性能不如Memcache opcache的性能优于其他几个缓存器 **一般会选择opcache+memcached|redis这个组合** 如果储存的量不大,tp 的file 缓存,和redis |memcached差不多。但是内存读取肯定要快。如果存储的值多,redis |memcached强大的i/o能力会强于 普通的文件读写 # [使用 OpCache 提升 PHP 5.5+ 程序性能](https://www.cnblogs.com/chunguang/p/5538093.html)(从编译层面提高) 作用是将PHP代码预编译生成的脚本文件 `Opcode` 缓存在共享内存中供以后反复使用,从而避免了从磁盘读取代码再次编译的时间消耗 省去了每次加载和解析 PHP 脚本的开销。同时,它还应用了一些代码优化模式,使得代码执行更快。从而加速PHP的执行(避免重复编译,减少 CPU 和内存开销) php.ini 打开 zend\_extension="D:\\phpStudy\\php\\php-7.2.14\\ext\\php\_opcache.dll" ~~~ [opcache] zend_extension = php_opcache.dll opcache.enable = 0 opcache.memory_consumption = 128 opcache.max_accelerated_files = 10000 opcache.revalidate_freq = 60 ; Required for Moodle opcache.use_cwd = 1 opcache.validate_timestamps = 1 opcache.save_comments = 1 opcache.enable_file_override = 0 ~~~ >[danger] 不要在编写调试代码中开启opcache,否则你会体会到你的增删改查不能即时同步的痛苦! 开启后在第一次访问php文件时候,自动编译并缓存,如果想手动编译缓存则使用下面的OPcache函数 # [**OPcache 函数**](https://www.php.net/manual/zh/ref.opcache.php) ## **opcache\_compile\_file**(string`$file`) :boolean 手动编译并缓存php脚本,即无需运行,即可编译并缓存 PHP 脚本 该函数可用于在 Web 服务器重启之后初始化缓存,以供后续请求调用。 ## **opcache\_invalidate**(string`$file`\[,boolean`$force`\=**`FALSE`**\] ) :boolean 废除php脚本的缓存 ## **opcache\_is\_script\_cached**(string`$file`) :bool 检查指定php脚本是否存在于opcache缓存中 ## **opcache_reset**(void) :boolean 该函数将重置整个字节码缓存。 在调用**opcache_reset()**之后,所有的脚本将会重新载入并且在下次被点击的时候重新解析。 ## **opcache\_get\_configuration**(void) :array 返回缓存实例的配置信息。 返回内容如下: ``` array(3) { ["directives"]=> array(25) { ["opcache.enable"]=> bool(true) ["opcache.enable_cli"]=> bool(true) ["opcache.use_cwd"]=> bool(true) ["opcache.validate_timestamps"]=> bool(true) ["opcache.inherited_hack"]=> bool(true) ["opcache.dups_fix"]=> bool(false) ["opcache.revalidate_path"]=> bool(false) ["opcache.log_verbosity_level"]=> int(1) ["opcache.memory_consumption"]=> int(134217728) ["opcache.interned_strings_buffer"]=> int(8) ["opcache.max_accelerated_files"]=> int(4000) ["opcache.max_wasted_percentage"]=> float(0.05) ["opcache.consistency_checks"]=> int(0) ["opcache.force_restart_timeout"]=> int(180) ["opcache.revalidate_freq"]=> int(60) ["opcache.preferred_memory_model"]=> string(0) "" ["opcache.blacklist_filename"]=> string(0) "" ["opcache.max_file_size"]=> int(0) ["opcache.error_log"]=> string(0) "" ["opcache.protect_memory"]=> bool(false) ["opcache.save_comments"]=> bool(true) ["opcache.load_comments"]=> bool(true) ["opcache.fast_shutdown"]=> bool(true) ["opcache.enable_file_override"]=> bool(false) ["opcache.optimization_level"]=> int(4294967295) } ["version"]=> array(2) { ["version"]=> string(9) "7.0.4-dev" ["opcache_product_name"]=> string(12) "Zend OPcache" } ["blacklist"]=> array(0) { } } ``` ## **opcache\_get\_status**(\[boolean`$get_scripts`\=**`TRUE`**\] ) :array 返回缓存实例的状态信息。 参数: `get_scripts`包含脚本的具体声明信息。 返回内容大致如下 ``` array(8) { ["opcache_enabled"]=> bool(true) ["cache_full"]=> bool(false) ["restart_pending"]=> bool(false) ["restart_in_progress"]=> bool(false) ["memory_usage"]=> array(4) { ["used_memory"]=> int(10936144) ["free_memory"]=> int(123281584) ["wasted_memory"]=> int(0) ["current_wasted_percentage"]=> float(0) } ["interned_strings_usage"]=> array(4) { ["buffer_size"]=> int(8388608) ["used_memory"]=> int(458480) ["free_memory"]=> int(7930128) ["number_of_strings"]=> int(5056) } ["opcache_statistics"]=> array(13) { ["num_cached_scripts"]=> int(1) ["num_cached_keys"]=> int(2) ["max_cached_keys"]=> int(7963) ["hits"]=> int(0) ["start_time"]=> int(1410858101) ["last_restart_time"]=> int(0) ["oom_restarts"]=> int(0) ["hash_restarts"]=> int(0) ["manual_restarts"]=> int(0) ["misses"]=> int(1) ["blacklist_misses"]=> int(0) ["blacklist_miss_ratio"]=> float(0) ["opcache_hit_rate"]=> float(0) } ["scripts"]=> array(1) { ["/var/www/opcache.php"]=> array(6) { ["full_path"]=> string(17) "/var/www/opcache.php" ["hits"]=> int(0) ["memory_consumption"]=> int(1064) ["last_used"]=> string(24) "Tue Sep 16 09:01:41 2014" ["last_used_timestamp"]=> int(1410858101) ["timestamp"]=> int(1410858099) } } } ```