How to use PHP OPCache?
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
:
zend_extension=/full/path/to/opcache.so (nix)
zend_extension=C:\path\to\php_opcache.dll (win)
Note that when the path contains spaces you should wrap it in quotes:
zend_extension="C:\Program Files\PHP5.5\ext\php_opcache.dll"
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.
var_dump(opcache_get_configuration());
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.
var_dump(opcache_get_status());
opcache_reset()
:
Resets the entire cache. Meaning all possible cached scripts will be parsed again on the next visit.
opcache_reset();
opcache_invalidate()
:
Invalidates a specific cached script. Meaning the script will be parsed again on the next visit.
opcache_invalidate('/path/to/script/to/invalidate.php', true);
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:
- OpCache status
- OpCache configuration
- OpCache statistics
- OpCache reset
- Cached scripts overview
- Cached scripts invalidation
- Multilingual
- Mobile device support
- Shiny graphs
Screenshots:
URL: https://github.com/PeeHaa/OpCacheGUI
opcache-status
Features:
- OpCache status
- OpCache configuration
- OpCache statistics
- Cached scripts overview
- Single file
Screenshot:
URL: https://github.com/rlerdorf/opcache-status
opcache-gui
Features:
- OpCache status
- OpCache configuration
- OpCache statistics
- OpCache reset
- Cached scripts overview
- Cached scripts invalidation
- Automatic refresh
Screenshot:
URL: https://github.com/amnuts/opcache-gui
How to precompile php opcache using command line?
May be someone will find this useful
Precompiling opcache for php-fpm via CLI does work
For some reason it takes too much time (compared with in-memory stored Opcodes) to load cached files into memory, but no recompilation of PHP files being executed.
P.S. This trick works even for different opcache confgurations.
Enable opcache for php in wamp
In PHP5.5.12 opcache is delivered as a zend extension but it is found in the standard ext
folder.
You would therefore load it just like any other PHP extension, apart from using the zend_extension
rather than extension
paramter, so edit your php.ini
file using the wampmanager menus to make sure you edit the right file like so :-
wampmanager -> PHP -> php.ini
First check that this parameter is set correctly :
extension_dir = "C:/wamp/bin/php/php5.5.12/ext/"
Now where you have loaded the OpCache dll in your example, do it like this and it will be loaded from the default extension folder just like a normal extension=
would be :-
zend_extension=php_opcache.dll
You could do it like this :-
zend_extension="C:/wamp/bin/php/php5.5.12/ext/php_opcache.dll"
but there is no need to specify the full path as it is loaded from the standard ext
folder.
Warning
If you are still developing you almost definitely don't what this turned on as it won't add any benefit and could add time to a standard compilation, recaching after every code change, and possibly not re-compiling and using the cached code when you don't want it to.
Enabling PHP7 Opcache
I fixed this by making sure apache get write to the folders I created. Once that was done restarting httpd made it work.
OpCache - understanding how the cache works, per user or per resource?
You are already right about how the opcode is stored in cache and read from the shared memory. It is however not on a user basis, but rather on a script level. Each php file that has been parsed and compiled to opcode will be saved to the shared memory and executed from there. The parsing and compiling steps, which are otherwise slow, will be shortcircuited for each script that has been cached.
To answer your questions, no the cache does not work on a per-user basis but rather on per-script. The cached opcode is still executed by the scripting engine (Zend mostly) per request.
And yes it does read from the shared memory.
Reference
D. Shafik: Everything You Need to Know About OpCode Caches
How can I enable opcache preloading in PHP 7.4?
According to the blog post this appears to be trivial. Apparently Symfony since 4.4 generates a preload script which has to be set in the php.ini
:
opcache.preload=/path/to/project/var/cache/prod/App_KernelProdContainer.preload.php
I did some tests in my local Docker environment and this is how it went:
PHP 7.3 without OPcache (current)
Requests per second: 8.75 [#/sec] (mean)
Time per request: 114.402 [ms] (mean)
PHP 7.4 without OPcache
Requests per second: 11.44 [#/sec] (mean)
Time per request: 87.417 [ms] (mean)
PHP 7.4 with OPcache, without preloading (Apache + modphp)
Requests per second: 30.25 [#/sec] (mean)
Time per request: 33.053 [ms]
PHP 7.4 with OPcache, without preloading (nginx + php fpm)
Requests per second: 40.00 [#/sec] (mean)
Unfortunately I was not able to enable the preloading :( I encountered following errors (in both Apache+Mod and Nginx+FPM):
double free or corruption (!prev)
child pid 17 exit signal Aborted (6), possible coredump (…)
This feature looks like a WIP though. I'm going to revalidate this answer when I'm able to use this preloading thing. In overall I'm quite impressed, +30% performance just by upgrading from PHP 7.3
to 7.4
.
Related Topics
PHP Redirect to Another Page After Form Submit
PHP: How to Check If Image File Exists
Prevent Back Button from Showing Post Confirmation Alert
How to Include() All PHP Files from a Directory
Remove Text Between Parentheses PHP
Find the Last Element of an Array While Using a Foreach Loop in PHP
Get Country of Ip Address With PHP
Transform Relative Path into Absolute Url Using PHP
Remove Portion of a String After a Certain Character
How to Get Code Point Number For a Given Character in a Utf-8 String
Include PHP Inside JavaScript (.Js) Files
How to Migrate My Site from MySQL to MySQLi
Efficiently Counting the Number of Lines of a Text File. (200Mb+)