opcache 简介
Zend opcache通过opcode缓存和优化提供更快的PHP执行过程。它将预编译的脚本文件存储在共享内存中供以后使用,从而避免了从磁盘读取代码并进行编译的时间消耗。同时,它还应用了一些代码优化模式,使得代码执行更快。(php >= 5.5)
启用opcache1
2
3
4
5
6
7
8
9
10zend_extension=opcache.so
opcache.enable=1 //启用opcache
opcache.enable_cli=1 //针对支持CLI版本PHP启动操作码缓存 一般被用来测试和调试
opcache.memory_consumption=128 //共享内存大小, 这个根据你们的需求可调
opcache.interned_strings_buffer=8 //interned string的内存大小, 也可调
opcache.max_accelerated_files=4000 //最大缓存的文件数目
opcache.validate_timestamps=1 //启用时间戳验证,配置opcache.revalidate_freq使用。设置0禁用,则必须手动重置OPcache或重新启动才能清除过期缓存
opcache.revalidate_freq=60 //60s检查一次文件更新
opcache.fast_shutdown=1 //打开快速关闭, 打开这个在PHP Request Shutdown的时候,会收内存的速度会提高
opcache.save_comments=0 //不保存文件/函数的注释
cachetool
如上设置60秒才自动清除过期缓存。这时要提交代码,不想重启php-fpm,想立即看到修改效果,可以使用cachetool清除所有opcache缓存。
https://github.com/gordalina/cachetool
CacheTool - 在CLI中管理缓存
CacheTool允许您通过cli使用apc,opcache和文件状态缓存。它将连接到fastcgi服务器(如php-fpm)并运行它的缓存。
为什么这么有用?也许你想清除字节码缓存而不重新加载php-fpm或使用web端点
也许你想要一个处理缓存失效的cron
也许你想直接从控制台看到一些统计数据
还有很多…
兼容性
CacheTool 4.x适用于PHP> = 7.1
CacheTool 3.x适用于PHP> = 5.5.9
CacheTool 2.x适用于PHP> = 5.5.9
CacheTool 1.x适用于PHP> = 5.3.3
下载1
2curl -sO http://gordalina.github.io/cachetool/downloads/cachetool.phar
chmod +x cachetool.phar
使用
CacheTool需要一个适配器才能连接,它可以是cli,fcgi和web。 fcgi适配器是最常见的,因为它直接连接到php-fpm。
您可以将IP地址或unix套接字传递给–fcgi适配器,或将其留空,CacheTool将尝试为您找到php-fpm套接字。如果找不到,则默认为127.0.0.1:9000。
您可以让CacheTool为您找到unix套接字,或默认为IP。
查看当前opcache配置1php cachetool.phar opcache:configuration --fcgi=127.0.0.1:9000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35Zend OPcache 7.1.3
+---------------------------------------+------------+
|Directive|Value|
+---------------------------------------+------------+
|opcache.enable|true|
|opcache.enable_cli|true|
|opcache.use_cwd|true|
|opcache.validate_timestamps|false|
|opcache.validate_permission|false|
|opcache.validate_root|false|
|opcache.inherited_hack|true|
|opcache.dups_fix|false|
|opcache.revalidate_path|false|
|opcache.log_verbosity_level|1|
|opcache.memory_consumption|134217728|
|opcache.interned_strings_buffer|8|
|opcache.max_accelerated_files|4000|
|opcache.max_wasted_percentage|0.05|
|opcache.consistency_checks|0|
|opcache.force_restart_timeout|180|
|opcache.revalidate_freq|0|
|opcache.preferred_memory_model|''|
|opcache.blacklist_filename|''|
|opcache.max_file_size|0|
|opcache.error_log|''|
|opcache.protect_memory|false|
|opcache.save_comments|true|
|opcache.fast_shutdown|true|
|opcache.enable_file_override|false|
|opcache.optimization_level|2147467263|
|opcache.lockfile_path|'/tmp'|
|opcache.file_cache|''|
|opcache.file_cache_only|false|
|opcache.file_cache_consistency_checks|true|
+---------------------------------------+------------+
查看opcache缓存状态1php cachetool.phar opcache:status --fcgi=127.0.0.1:9000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24+----------------------+---------------------------------+
|Name|Value|
+----------------------+---------------------------------+
|Enabled|Yes|
|Cache full|No|
|Restart pending|No|
|Restart in progress|No|
|Memory used|15.13 MiB|
|Memory free|112.87 MiB|
|Memory wasted (%)|0 b (0%)|
+----------------------+---------------------------------+
|Cached scripts|754|
|Cached keys|1394|
|Max cached keys|7963|
|Start time|Wed, 01 Aug 2018 03:57:43 +0000|
|Last restart time|Wed, 01 Aug 2018 05:49:38 +0000|
|Oom restarts|0|
|Hash restarts|0|
|Manual restarts|7|
|Hits|582|
|Misses|766|
|Blacklist misses (%)|0 (0%)|
|Opcache hit rate|43.175074183976|
+----------------------+---------------------------------+
查看opcache缓存文件1php cachetool.phar opcache:status:scripts --fcgi=127.0.0.1:9000
1
2
3
4
5
6
7
8+------+------------+---------------------------------------------------------------------------------------------------------------------------+
| Hits | Memory | Filename |
+------+------------+---------------------------------------------------------------------------------------------------------------------------+
| 0 | 1.92 KiB | /data/web/demo.sundayle.com/app/Repositories/Contracts/IndexRepository.php |
| 1 | 3.52 KiB | /data/web/demo.sundayle.com/routes/admin/user.php |
| 1 | 83.82 KiB | /data/web/demo.sundayle.com/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php |
| 1 | 9.75 KiB | /data/web/demo.sundayle.com/vendor/symfony/http-foundation/ServerBag.php |
+------+------------+---------------------------------------------------------------------------------------------------------------------------+
清除opcache缓存文件1php cachetool.phar opcache:reset --fcgi=127.0.0.1:9000
查看opcache清除后效果1
2
3
4php cachetool.phar opcache:status:scripts --fcgi=127.0.0.1:9000
+------+--------+----------+
| Hits | Memory | Filename |
+------+--------+----------+
帮助1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59php cachetool.phar list
CacheTool 3.2.1-2-ga819a69
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--fcgi[=FCGI] If specified, used as a connection string to FastCGI server.
--fcgi-chroot[=FCGI-CHROOT] If specified, used for mapping script path to chrooted FastCGI server. --tmp-dir need to be chrooted too.
--cli If specified, forces adapter to cli
--web If specified, forces adapter to web
--web-path[=WEB-PATH] If specified, used as a information for web adapter
--web-url[=WEB-URL] If specified, used as a information for web adapter
-t, --tmp-dir=TMP-DIR Temporary directory to write files to
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
help Displays help for a command
list Lists commands
self-update [selfupdate] Updates cachetool.phar to the latest version
apc
apc:bin:dump Get a binary dump of files and user variables
apc:bin:load Load a binary dump into the APC file and user variables
apc:cache:clear Clears APC cache (user, system or all)
apc:cache:info Shows APC user & system cache information
apc:cache:info:file Shows APC file cache information
apc:key:delete Deletes an APC key
apc:key:exists Checks if an APC key exists
apc:key:fetch Shows the content of an APC key
apc:key:store Store an APC key with given value
apc:regexp:delete Deletes all APC key matching a regexp
apc:sma:info Show APC shared memory allocation information
apcu
apcu:cache:clear Clears APCu cache
apcu:cache:info Shows APCu user & system cache information
apcu:cache:info:keys Shows APCu keys cache information
apcu:key:delete Deletes an APCu key
apcu:key:exists Checks if an APCu key exists
apcu:key:fetch Shows the content of an APCu key
apcu:key:store Store an APCu key with given value
apcu:regexp:delete Deletes all APCu key matching a regexp
apcu:sma:info Show APCu shared memory allocation information
opcache
opcache:configuration Get configuration information about the cache
opcache:invalidate:scripts Remove scripts from the opcode cache
opcache:reset Resets the contents of the opcode cache
opcache:status Show summary information about the opcode cache
opcache:status:scripts Show scripts in the opcode cache
stat
stat:clear Clears the file status cache, including the realpath cache
stat:realpath_get Show summary information of realpath cache entries
stat:realpath_size Display size of realpath cache