OPcache and the PHP Function opcache_Reset
The OPcache extension implements various functionality to speed up PHP in a transparent manner. This includes opcode caching, an optimizer, and a just-in-time compiler. This article will focus on opcode caching.
The primary function of opcache is to cache compilation artifacts in shared memory, so that they are available the next time PHP needs to execute a script, eliminating the need to recompile the script. This improves performance drastically.
When a script is loaded, the compilation of the class is done as usual, except that it will only be cached if all its dependencies are also present. Inheritance is handled by opcache in a similar fashion as normal, and preloading supports complicated inheritance scenarios (including variance cycles).
On Unix-like systems opcache maintains a single fixed-size SHM segment, which is shared between all processes that make use of it. However, opcache doesn't necessarily guarantee that the address of internal functions/classes will be the same in each process, as the engine implementation may change the addresses used.
To work around this, opcache uses a file cache fallback that is enabled by default. This allows the cached artifacts to be used by all processes, but it has the disadvantage that it requires a write lock to be maintained.
To avoid the overhead of locking, opcache implements a simple API for invalidating an individual script from the opcode cache. The opcache_invalidate() function takes two arguments: the path of the script to be invalidated, and the optional argument force. The function will return TRUE if the script has been invalidated, or FALSE if there is no need to invalidate it. In some cases, it might be necessary to clear the opcode cache manually. For example, when deploying a new version of an application. In such a scenario, it is recommended to use the opcache_reset() function.