6

OPcacheでPHPを使用しています。私はマスターに git-push して Web サイトを本番環境にデプロイするだけです (実際には、単体テストの直後ですが、気にしないでください)。ファイルではphp.ini、OPcache の設定は「時間」と「頻度」に関するものです。しかし、サーバーで git pull を実行した後にキャッシュをリセットしたいだけです。

opcache_resetしたがって、本番サーバーでgit-pullの後に呼び出して(キャッシュをリセットしない)に設定opcache.validate_timestampsするだけでよいと思います0

私はその方法について何も読んでいなかったので、疑問に思っています。それが良い習慣であるかどうかはわかりません。私は何か見落としてますか?リスクはありますか、それとも大丈夫ですか?

どうもありがとう!

PS:私はPHPフレームワークとコンポーザを使用しています(composer installgit-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 に答える