Opcache "validate_timestamps" is disabled on our servers for the best performance.

With this directive disabled, you must reset OPcache manually via opcache_reset(), opcache_invalidate() or until the server is restarted for changes to the filesystem to take effect.

Yes, it is a pain in the ass, but you should use it.

Why?

Well, the problem with deploying new code to a running server is quite simple to understand.

A request that starts on one version of the code might access other files during the request and if those files are updated to a new version during the request you end up with strange side effects.

In PHP you might autoload files on demand when instantiating objects and if the code has changed mid-request the caller and the implementation could easily get out of synch.

To make the above easier, it means that while you're updating or deploying code, new code files may get mixed with old ones — the results are unknown.

So it's unsafe for a production environment.

In a development or staging environment, having "opcache.validate_timestamps=1" and "opcache.revalidate_freq=2" is OK because scripts will be stated on every compile request to see if they are changed.

If you encounter weird problems (making changes and not seeing them), you may need to also tinker with OpCache.

One of the ways to flush Opcache is to create a PHP file called flush_cache.php (attached) in your docroot with content like the one found at this Dropbox link:

  opcache_reset();

Every time you want to flush your Opcache, you can browse to that file and it'll call opcache_reset(); for your entire Opcache (VPS or dedicated machines).

The next PHP request to your site will populate the cache again.

Another thing you could do is to add below to your .htacess file:

 

  <FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|mp3|mp4|png|pdf|swf|txt)$">
    
      ExpiresActive Off
    
    
      FileETag None
      Header unset ETag
      Header unset Pragma
      Header unset Cache-Control
      Header unset Last-Modified
      Header set Pragma "no-cache"
      Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
      Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
    
  

The "FileETag None" and "Header unset ETag" not really applicable since these are not turned on but there because you never can tell

OR

# DISABLE CACHING
  
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
  

We strongly recommend that you only edit your configuration files with cPanel's MultiPHP INI Editor interface (cPanel >> Home >> Software >> MultiPHP INI Editor).

Another method of turning opcache off is adding the below to your htaccess:

  php_flag opcache.enable Off

Remember that .htaccess files are read from top to bottom, so if you want these directives to occur before something else, then they should be placed higher within your .htaccess file.

Also, it is important to point out that there may be multiple .htaccess files and that .htaccess files placed above another in a directory will have precedence over the one placed in a sub-directory.

この回答は役に立ちましたか? 1 好評の記事 (3 投票)