Current Location: Home> Latest Articles> How to Use opcache_get_status with opcache_invalidate to Dynamically Manage and Control PHP Cache

How to Use opcache_get_status with opcache_invalidate to Dynamically Manage and Control PHP Cache

gitbox 2025-09-09

<?php
/*
Article Title: How to Use opcache_get_status with opcache_invalidate to Dynamically Manage and Control PHP Cache

In high-performance PHP applications, Opcache is a key tool for improving code execution efficiency. By caching compiled PHP bytecode, Opcache significantly reduces file parsing and compilation overhead. However, in dynamic development scenarios, we may need finer control over cached content, such as refreshing the cache when code updates occur or under certain conditions. PHP provides two very useful functions: opcache_get_status and opcache_invalidate, which help us dynamically manage and control Opcache cache.

  1. Introduction to opcache_get_status
    opcache_get_status is a function used to retrieve the current status of Opcache. It returns an associative array containing information about the Opcache, including the list of cached scripts, cache hit rates, memory usage, and more.

Example code:
*/

$status = opcache_get_status();
echo "

"</span></span><span>;<br>
</span><span><span class="function_ invoke__">print_r</span></span><span>(</span><span><span>$status</span></span><span>);<br>
</span><span><span>echo</span></span><span> </span><span><span>"
";

/*
From the output above, we can view:

  • scripts: All PHP scripts currently cached and their status

  • memory_usage: Memory details used by Opcache

  • hits/misses: Cache hit rate information

  1. Introduction to opcache_invalidate
    opcache_invalidate is used to clear the Opcache for a specific script, allowing PHP to recompile the latest code. The function accepts two parameters:

  2. File path (absolute or relative)

  3. Force refresh (boolean, true forces a refresh even if the file hasn’t changed)

Example code:
*/

$file = FILE; // Current file
if (opcache_invalidate($file, true)) {
echo "Cache refreshed: $file\n";
}
else {
echo "Cache refresh failed or file not cached: $file\n";
}

/*
3. Combined Use: Dynamic PHP Cache Management
In practice, we can combine opcache_get_status and opcache_invalidate to implement dynamic cache management. For example:

  1. Iterate through cached scripts to find those that need refreshing

  2. Trigger opcache_invalidate based on conditions to ensure the latest code takes effect

  3. Monitor Opcache hit rates and memory usage to optimize cache strategy

Example code:
*/

$status = opcache_get_status();
if (isset($status['scripts']) && is_array($status['scripts'])) {
foreach ($status['scripts'] as $script => $info) {
// Condition example: Clear cache if not refreshed for over 1 day
$last_used = $info['last_used'];
if (time() - $last_used > 86400) { // 24 hours
opcache_invalidate($script, true);
echo "Cache refreshed: $script\n";
}
}
}

/*
4. Practical Recommendations

  1. Regularly monitor Opcache status and set appropriate memory size and caching strategy.

  2. In development environments, frequently use opcache_invalidate to refresh cache, ensuring code updates take effect immediately.

  3. In production environments, minimize unnecessary cache refreshes to maintain performance benefits.

  4. Consider creating custom functions or management classes to unify cache refresh operations and implement intelligent control with logging and monitoring.

Summary:
By using opcache_get_status to retrieve cache status and combining it with opcache_invalidate for specific scripts, we can manage PHP cache more flexibly, ensuring timely code updates while maintaining high application performance.
*/
?>