Slow file recognition on MAMP, PHP7 and OPcache

Posted on 28/02/2016

I reinstalled MAMP recently, in order to get PHP7 and latest updates in MAMP. At first, everything worked out of the box and even the previous databases were recognized without any issues. However, after I started working, I realized that there was a lag before my code would be recognized.

Since I was working in Drupal 8, I tried disabling every possible caching settings in the site configuration, settings.local.php as well as uninstalling all caching related modules. However, this didn't help at all, and I started looking into PHP setup and disabling the op-code caching.

The problem was in revalidate_freq configuration for OPcache. OPcache is the successor to APC module for PHP and is used for improving the PHP performance. In a nutshell, it compiles your PHP scripts, keeps them in the memory and executes them faster because it avoids parsing and compiling the source code during each request. This works great and the performance improvements are immediately noticeable.

However, the defaults shipped with MAMP can cause you some trouble. The default value of revalidate_freq is set to 60, meaning that OPcache will keep your files compiled in memory for 60 seconds, and only after that it will reload and recompile the source. This means that if you update the files within these 60 seconds, the changes will not be recognized immediately.

To fix this, open: /Applications/MAMP/bin/php/php/7.0.0/conf/php.ini and update the following line:

  1. opcache.revalidate_freq=0

That's all. On a production server you might want to set something other than 0, but on development machines you will definitely want this to be 0.

Refer to the opcache.configuration docs for more information.