6

我将 PHP 与 OPcache 一起使用。我只 git-push 来掌握在生产中部署我的网站(不是真的,只是在单元测试之后,但没关系)。在php.ini文件中,OPcache 设置是关于“时间”和“频率”的。但我只想在我的服务器上 git pull 后重置缓存。

所以我想我只需要opcache_reset在我的生产服务器上调用 git-pull 并设置opcache.validate_timestamps0(从不重置缓存)

我没有读过任何关于这种方式的信息,所以我怀疑:我不知道这是否是一个好习惯。我错过了什么?有风险还是可以?

非常感谢!

PS:我正在使用 PHP 框架和作曲家(composer install在 git-pull 之后运行)

4

1 回答 1

2

In order to get the greatest benefit from OPCache you should disable opcache.validate_timestamps. If you subsequently call opcache_reset() from a script every time you deploy your code to the server, then your OPCache is cleared once for each new set of files, and the system doesn't waste resources constantly checking the files.

There's a couple of "gotchas", however:

First of all, Make sure that the call to opcache_reset() happens, or else you'll be running the old code. If you have a script to execute your deploy, make sure it fails loudly if this step doesn't execute.

Secondly, depending on exactly how PHP is running (mod_php vs php-fpm), you may need to execute the opcache_reset() function via a request to the browser, not via the command line. For example, the most obvious solution to clear the cache is to have a simple PHP file like the following

<?php

if (php_sapi() != "cli") die("Not accessible from web");
opcache_clear();

and execute that file on each code pull. Depending on the version of PHP and how it's run that may only clear the cache for the command line and not for your running web version.

If clearing from the command line doesn't work, consider creating a similar script and calling it via the web using curl or wget. For example, curl http://example.com/clear_cache.php?secret=abc123. If you create the script to be web accessible, then make sure it checks a secret key to prevent someone from loading up your server by constantly clearing your cache.

Finally, as others have suggested, to make your builds totally repeatable between testing and deployment, consider having the end of the test process create a .zip file of the entire code used for testing, including the libraries pulled down by composer. Rather than git pull on your server, just unzip the file over the code root. I realize that git pull && composer update is easy. However, as others have suggested, if a library gets updated between the time tests were run and the time of deployment, then your code may no longer work as expected.

于 2016-07-22T17:16:39.850 回答