What is a Opcache

My official group click here.

Every time I perform a PHP script, the script needs to be compiled into bytecode, and OPcache can of the bytecode cache, in this way, the next request the same script, the script will not need to recompile, this greatly saved script execution time, allowing applications faster and at the same time saves the cost of server.

Speak in numbers

We would certainly like to know exactly what tuning is going on, and while performance improvements are highly dependent on application and server configuration, we can get a general idea by running a benchmark test.

For this purpose, I prepared a very low-profile machine: 1 core CPU, 1 gigabyte of memory to run the Apache benchmark. I requested the Laravel 5.4 default welcome page and let 10 concurrent requests continue for 1 minute. Here are the benchmark results for turning off OPcache:

OPcache disabled: 10.18 requests per secondCopy the code

Not too bad for such a low-profile server, but we can do better. The benchmark test results for enabling OPcache are as follows (using the default OPcache configuration) :

Enabled with default values: 34.52 requests per secondCopy the code

That’s a big gap! We then optimized the OPcache configuration and the benchmark performance was even better:

Enabled with optimized values: 42.53 requests per secondCopy the code

Have you taken this one yet?

Sounds cool, but how do you use it

First, we need to ensure that OPcache is installed on the server. OPcache has been part of the PHP core since PHP 5.5, so there is little need for Laravel developers to install the extension manually.

Of course, if you are worried, you can check phpInfo () for confirmation:

This script displays extensions for all PHP installations. Search the page for “OPcache”. If it is found, it is installed. If no, install it yourself.

Next, we need to enable OPcache in the PHP configuration file (off by default) :

opcache.enable=1 Copy the code

Let’s continue with some optimizations for OPcache:

opcache.memory_consumption=512

This configuration indicates the amount of memory (MB) you want to allocate to OPcache. Set a value greater than 64.

opcache.interned_strings_buffer=64Copy the code

This configuration represents the amount of space (in MB) you want to allocate to the actual string, with a value greater than 16.

opcache.max_accelerated_files=32531Copy the code

This configuration indicates how many scripts can be cached, setting this value as close to (or greater than) the number of scripts contained in the project as possible.

opcache.validate_timestamps=0Copy the code

Changing the configuration value is used to revalidate the script. If it is set to 0 (for best performance), OPcache needs to be manually cleared after every PHP code change. If you don’t want to clear it manually, you can set it to 1 and configure the revalidation interval with opCache.revalidate_freq, which can cost some performance as changes need to be checked every x seconds.

opcache.save_comments=1 Copy the code

This configuration preserves comments in scripts, and I recommend turning it on because some libraries depend on it, and I don’t see much benefit in turning it off.

opcache.fast_shutdown=0Copy the code

Fast shutdowns give a faster mechanism for cleaning up memory, but, in my benchmark tests, slower, which may give the application some performance gains, but you’ll have to try it yourself.

So, the final configuration optimization looks like this:

opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=32531
opcache.validate_timestamps=0
opcache.save_comments=1
opcache.fast_shutdown=0Copy the code

You can experiment with these configuration values, depending on your application size and server configuration.

Finally, save the configuration file and restart the Web server, and your application will definitely get faster.

Get the Laravel app ready

If opcache.validate_timestamps is set to 0, we need to manually clear opCache every time we change the PHP code. To do this, I created an extension pack that provides the Artisan command to handle OPcache cleanup: github.com/appstract/l… .

After the extension is installed, simply run the following command to clear OPcache:

php artisan opcache:clear Copy the code

To learn more, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory directory, as long as you finish the guarantee salary rise a step (continue to update)


I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.