OpCache – Install PHP 5.5+January 11th, 2014
PHP is an interpreted language which means that each time a PHP generated page is requested, the server reads the various files needed and “compiles” them into something the machine can understand (opcode). Opcode Caches are performance enhancing extensions for PHP which cache the execution cycle of a PHP script for later re-use. Subsequent script requests are fulfilled by the cache meaning that the PHP engine does not need to spend time re-compiling the same script every time it is requested. Opcode caching can make a huge difference to the speed of a PHP website, and it is not uncommon to see a 3x performance increase just by enabling an OpCode cache.
PHP 5.5+ OpCache
For many years, I used Alternative PHP Cache as an “out of the box” caching solution. PHP 5.5+ now ships with its very own caching mechanism called OpCache, designed to replace the APC module.
However it is disabled by default and in order to start using OpCache in PHP5.5+ you will have to enable it:
Firstly, you need to locate your opcache.so file. Mine was found in:
Add the following line to your php.ini file if you using a Unix Server:
Add the following line to your php.ini file if you using a Windows Server:
Note that if your path contains spaces, you need to wrap them in quotes like:
You now need to restart Apache for the change to be successful.
Currently there are four functions which you can use:
var_dump(opcache_get_configuration()); var_dump(opcache_get_status()); opcache_invalidate('/path/to/script/to/invalidate.php', true); opcache_reset();
This will return an array with information about the current status of the cache. This information will include things like: the state the cache is in (enabled, restarting, full etc), the memory usage, hits, misses and some more useful information. It will also contain the cached scripts. After you have restarted Apache, phpinfo() should contain the following new section:
Checking that Opcode Cache is running
You can see from the above screenshot from phpinfo() that we have a value in the “Used Memory” as well as values for “Cache Hits” and “Cache Misses”. A cache miss, generally, is when something is looked up in the cache and is not found. The cache did not contain the item being looked up. The cache hit is when you look something up in a cache and it was storing the item and is able to satisfy the query. The following code can be run to prove that the extension is working:
BTW: I don’t suggest that you run “var_dump(opcache_get_status());” because it outputs an array of every cached file in the cache and on a large, live site this could be lots of data!