Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created July 4, 2017 16:44
Show Gist options
  • Save tzkmx/0cc7806817cac7500e2e2bfd9eef6405 to your computer and use it in GitHub Desktop.
Save tzkmx/0cc7806817cac7500e2e2bfd9eef6405 to your computer and use it in GitHub Desktop.
OpCache path invalidation script
<?php
// Untested, retrieved and reformatted from:
// https://ihaveabackup.net/article/invalidating-the-opcache-in-php-5-5#comment-19
if (!function_exists('opcache_get_configuration')) {
die('OPCache is not installed/running');
}
$data = opcache_get_status(); $expired = array();
if (empty($data['scripts'])) {
die('no cached files');
}
/** Opportunity:
* taking a hint passed from query argument, of path prefix instead of checking the whole universe of cached scripts
*/
foreach ($data['scripts'] as $file) {
if (
!empty($file['timestamp'])
&& !empty($file['full_path'])
&& (!file_exists($file['full_path']) || (int)$file['timestamp'] < filemtime($file['full_path']))
) {
$expired[]=$file['full_path'];
opcache_invalidate($file['full_path'], true);
}
}
/** Opportunity: display invalidated entries */
echo count($expired),' deleted';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment