PHP 5.5 has been released and it features a new code caching module called OPCache, but there doesn't appear to be any documentation for it.
So where is the documentation for it and how do I use OPcache?
PHP 5.5 has been released and it features a new code caching module called OPCache, but there doesn't appear to be any documentation for it.
So where is the documentation for it and how do I use OPcache?
I encountered this when setting up moodle. I added the following lines in the php.ini file.
intl -> http://php.net/manual/en/book.intl.php
Installation
OpCache is compiled by default on PHP5.5+. However it is disabled by default. In order to start using OpCache in PHP5.5+ you will first have to enable it. To do this you would have to do the following.
Add the following line to your
php.ini
:Note that when the path contains spaces you should wrap it in quotes:
Also note that you will have to use the
zend_extension
directive instead of the "normal"extension
directive because it affects the actual Zend engine (i.e. the thing that runs PHP).Usage
Currently there are four functions which you can use:
opcache_get_configuration()
:Returns an array containing the currently used configuration OpCache uses. This includes all ini settings as well as version information and blacklisted files.
opcache_get_status()
: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.
opcache_reset()
:Resets the entire cache. Meaning all possible cached scripts will be parsed again on the next visit.
opcache_invalidate()
:Invalidates a specific cached script. Meaning the script will be parsed again on the next visit.
Maintenance and reports
There are some GUI's created to help maintain OpCache and generate useful reports. These tools leverage the above functions.
OpCacheGUI
Disclaimer I am the author of this project
Features:
Screenshots:
URL: https://github.com/PeeHaa/OpCacheGUI
opcache-status
Features:
Screenshot:
URL: https://github.com/rlerdorf/opcache-status
opcache-gui
Features:
Screenshot:
URL: https://github.com/amnuts/opcache-gui
OPcache replaces APC
Because OPcache is designed to replace the APC module, it is not possible to run them in parallel in PHP. This is fine for caching PHP opcode as neither affects how you write code.
However it means that if you are currently using APC to store other data (through the
apc_store()
function) you will not be able to do that if you decide to use OPCache.You will need to use another library such as either APCu or Yac which both store data in shared PHP memory, or switch to use something like memcached, which stores data in memory in a separate process to PHP.
Also, OPcache has no equivalent of the upload progress meter present in APC. Instead you should use the Session Upload Progress.
Settings for OPcache
The documentation for OPcache can be found here with all of the configuration options listed here. The recommended settings are:
If you use any library or code that uses code annotations you must enable save comments:
I am going to drop in my two cents for what I use opcache.
I have made an extensive framework with a lot of fields and validation methods and enums to be able to talk to my database.
Without opcache
When using this script without opcache and I push 9000 requests in 2.8 seconds to the apache server it maxes out at 90-100% cpu for 70-80 seconds until it catches up with all the requests.
Total time taken: 76085 milliseconds(76 seconds)
With opcache enabled
With opcache enabled it runs at 25-30% cpu time for about 25 seconds and never passes 25% cpu use.
Total time taken: 26490 milliseconds(26 seconds)
I have made an opcache blacklist file to disable the caching of everything except the framework which is all static and doesnt need changing of functionality. I choose explicitly for just the framework files so that I could develop without worrying about reloading/validating the cache files. Having everything cached saves a second on the total of the requests
25546 milliseconds
This significantly expands the amount of data/requests I can handle per second without the server even breaking a sweat.
With PHP 5.6 on Amazon Linux (should be the same on RedHat or CentOS):
and then restart apache.