3

For sites that have PHP OPCache enabled:

  1. Does updating site using Drush require restarting the webserver to invalidate the scripts cached by OPCache?
  2. Does Drupal core or Drush invalidate OPCache before running updates?
  3. Is setting opcache.enable_cli to 0 a better strategy in this scenario?
2
  • 2
    I don't know the answer specifically, but I know that when I have opcache enabled, I don't have to do anything special to flush classes after doing updates or clearing the registry. However, I've never actually confirmed that this is because nothing needs to be done, or because I haven't properly configured opcache. – Jaypan Feb 19 at 21:26
  • This probably can easily be found out in a reproducible Docker setup. – leymannx Feb 20 at 13:51
3

Updated response to address the questions as asked.

  1. Yes, either the web server need to be restarted or the web-server's OPCache needs to be cleared. Drush cannot clear the web-server's opcache as it is a CLI application. Instead of restarting the web server a php script with following content can be added to next to Drupal's update.php

     <?php
     opcache_reset( );
     /*
     Following measures should be taken to safeguard this file from malicious use
     a) Set filename with a long random string as a name (like 100+ characters).
     b) Check the IP address of the requester so the script can only be called from trusted networks.
    
     To clear cache visit this file at 
     siteurl/random_long_string_eiquooR3.php
     or use curl siteurl/random_long_string_eiquooR3.php
     Check the IP address of the requester so it can only be called from trusted networks.
     */
    
  2. Neither Drupal core nor Drush invalidate OPCache.

  3. Yes, setting opcache.enable_cli to 0 is a fine, it is anyways off by default. Unless you are really explicit in your configuration of opcache to enable it on the cli and also use a file based caching mechanism, the opcache exists in memory and is reset on each command line invocation. Therefore you only need to worry about clearing opcache by restarting any long-running PHP command line jobs.


Original Answer by @greggles

  1. Restarting the webserver works, but takes quite some time. A more efficient solution is to have a way to call opcache_reset.
  2. Opcache must be reset by calling opcache_reset from within the web process. If you call it inside drush then it will not clear the opcache for the webserver so it won't help the problem.
  3. Unless you are really explicit in your configuration of opcache to enable it on the cli and also use a file based caching mechanism, the opcache exists in memory and is reset on each command line invocation. Therefore you only need to worry about clearing opcache by restarting any long-running PHP command line jobs.

If you want to clear opcache as part of a deploy process, I suggest adding a file that contains opcache_reset and then you have to protect access to that file so malicious users can't easily call it:

  1. Give it a long random string as a name (like 100+ characters).
  2. Check the IP address of the requester it can only be called from trusted networks.
4
  • thanks for sharing the information. Yes, pretty clear that the opcache_reset needs to be performed from the web. However, my question is pertaining to drush? From experience it appears drush may be performing a cli-opcache flush when running updatedb command, but I have not looked at the drush code yet. – amit Feb 20 at 20:08
  • 1
    I updated my answer item 3 to cover opcache cli behavior a bit more. Drush does not perform any opcache flush by default and it's generally not needed due to the way opcache works for cli. – greggles Feb 22 at 15:43
  • @amit In the case it's not clear, greggles' comment is saying that Opcode cache is disabled for CLI. Since Drush is a CLI command, it wouldn't make sense to have a Drush command to clear Opcode cache, since it would be disabled for it. – apaderno Mar 1 at 19:57
  • I have rephrased @greggles answers, feel free to tweak it before I accept it. – amit Mar 2 at 7:30
0

We just ended up writing a more general solution to purge PHP and Database cache's used by Drupal Core. This module provides three ways to purge Opcache.

  • Web browser user interface
  • Curl commands
  • Drush commands

The Cache Utility module is available on Drupal.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.