Question

For opcode cache (php 5.5) invalidation I wrote a small script. However, the status does tell me the invalidation failed. How does this happen?

<?php
$scripts  = opcache_get_status(true)["scripts"];    
$failures = array();
foreach (array_keys($scripts) as $file) {
    $result = opcache_invalidate($file, true);
    if (!$result) $failures[] = $file;
}
if(count($failures) !== 0) {
    exit("Failed to clear OPcache for files " . implode(", ", $failures));
}

echo sprintf("%s OPcache files cleared\n", count($scripts));

$scripts = opcache_get_status(true)["scripts"];
echo sprintf("There are still %s files in the cache", count($scripts));

It's now a very simple script (which will be part of Soflomo\Cache). My problem is that the first echo ("x OPcache files cleared for web") prints the same number as the last echo ("There are still x files in the cache") statement!

How to reproduce:

  1. Install a big FW app (using ZF2 here)
  2. Place above file in the webroot of the app
  3. Run the app (500+ files will get cached)
  4. Run the script, the output will be similar to:

560 OPcache files cleared

There are still 560 files in the cache

Can anyone explain this? And are the files cleared for sure, or not?

PS. I know the above could be replaced by opcache_reset(), but the script will filter on a certain subset of files as a next step. It is meant to clear the opcode for files from a single application, even if there are multiple applications running on a single server. The reset will clear all those files, which isn't an option.

Was it helpful?

Solution

Opcache does not evict invalid items from memory - they stay there until the pool is full at which point the memory is completely cleared. Existence of an invalid cache entry does not prevent PHP loading/compiling the file again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top