For websites based on WordPress, Joomla or Laravel, for example, which do not have any user-specific content, it is recommended to keep all pages in a static HTML cache and to regenerate the cache manually (or automatically) only when changes are made in the backend. However, if the page contains dynamic content that depends on sessions and cookies or even language and location, the use of OPcache is suitable.
The so-called boot time for all three systems mentioned above is over 100 ms on a freshly installed CentOS7 server (with SSD and PHP 7.1). Each time a PHP script is executed, it must be compiled into bytecode at runtime. OPcache now implements a cache for this bytecode - if the same PHP script is now executed on the next request, it does not need to be recompiled. After activating OPcache, we set the following settings:
9006cec6c5d3d2498ee26c5bfd91590a
The execution time (averaged over 10 executions) has now been significantly reduced:
WordPress 4.7.4 | Joomla 3.7 | Laravel 5.4 | |
without OPcache | 295 ms | 201 ms | 110 ms |
with OPcache | 54 ms | 59 ms | 13 ms |
Factor | 5,5 | 3,5 | 8,5 |
Since we set validate_timestamps to 0 and revalidate_freq to the duration of one day, the cache is practically never rebuilt automatically (we do this manually on purpose). With a small git-hook, a PHP script is executed with every "pull". To activate the hook:
a1678610c8b4c7db5997375944183830
a1678610c8b4c7db5997375944183830
The following helper function is also helpful to measure single code parts (or the total execution time):
085f4291bbcc4e8a64bbea951ff49ab9
If you want to get helpful information like status, used memory or cached file number, use this PHP function:
3f6309d04bc7e48dd909859321b96209
To get the benefit of OPcache also in a local development environment (which is generally not recommended), we use a trick
OPcache is active on all servers that have the OPcache module installed and on which the opcache.enable setting is not explicitly set. We leave it that way. We only set opcache.validate_timestamps = 1 in php.ini. This means that opcache.revalidate_freq = 2. This means that OPcache is revalidated almost continuously for every project. For those projects in which we want to activate OPcache permanently, we use project-related in the .htaccess the following setting:
1dd64a3f2fe4425b29b1409410fe3fc0
Then we restart the web server and create the following script:
91fb5289160e410607c9ea89112a575b
This script clears the cache and rebuilds it at the same time. Now we set a workspace-related RunOnSave command in our favorite editor VSCode:
91fb5289160e410607c9ea89112a575b