In PHP development, the realpath() function is a very commonly used path resolution function. To improve performance, PHP will cache the results of realpath() call, which is called "realpath cache". The two key functions for managing and viewing this cache are realpath_cache_size and realpath_cache_get . This article will introduce the functions and differences between these two functions, and share some practical tips for their joint use.
realpath_cache_size() is a very intuitive function that returns the memory size (in bytes) used by the current realpath cache.
<?php
echo realpath_cache_size();
?>
This function does not accept parameters, the return value is an integer, indicating the total footprint of the current realpath cache.
Unlike realpath_cache_size() , the realpath_cache_get() function returns the actual content of the cache, including detailed information of all paths in the cache, such as the real address of the path, whether it is a directory, expiration time, etc.
<?php
print_r(realpath_cache_get());
?>
The return value is an associative array, the key of the array is the path in the cache, and the value is an array containing the following fields:
key : The unique identifier of the cache item (usually a path)
is_dir : Is it a directory
realpath : real path
expires : cached expiration time (timestamp)
Array
(
[/var/www/html/index.php] => Array
(
[key] => /var/www/html/index.php
[is_dir] =>
[realpath] => /var/www/html/index.php
[expires] => 1716626500
)
)
characteristic | realpath_cache_size | realpath_cache_get |
---|---|---|
effect | Return cache size | Return to cached content |
Return value type | Integer | Associative array |
parameter | none | none |
Typical uses | Performance monitoring/memory optimization | Debug cached content |
realpath_cache_size() is mainly used for performance monitoring or memory usage analysis, while realpath_cache_get() is more suitable for debugging path resolution and caching problems.
During development, if you notice that the value returned by realpath_cache_size() is constantly increasing, you can combine realpath_cache_get() to see which paths are cached. Monitor regularly using the following script:
<?php
echo "Cache size: " . realpath_cache_size() . " bytes\n";
print_r(realpath_cache_get());
?>
By comparing the output, analyze whether unnecessary paths are frequently parsed and added to the cache.
In php.ini , realpath_cache_size is 16K by default. For large projects, this value may be too small, resulting in a decrease in cache hit rate. You can use the following code to help decide whether you need to expand this value:
<?php
$cache = realpath_cache_get();
$used = realpath_cache_size();
if ($used > 15000) {
echo "Recommended to addrealpath_cache_sizeValue of,Currently used {$used} byte\n";
}
?>
When using require , include or file_exists to encounter path resolution problems, you can directly check whether the specified path is in the cache and whether the cache has expired through realpath_cache_get() . For example:
<?php
$path = '/var/www/html/config.php';
$cache = realpath_cache_get();
if (isset($cache[$path])) {
echo "This path is cached:\n";
print_r($cache[$path]);
} else {
echo "The path is not cached or expired\n";
}
?>
This method is more efficient and informative than traditional debugging methods.
In high concurrency scenarios, appropriately increasing the value of realpath_cache_size can effectively reduce the IO operation of the file system.
Use realpath_cache_get() with logging to monitor and optimize path resolution behavior in frameworks or large projects.
Note that realpath_cache_get() returns a snapshot of internal cache in PHP, and the performance may vary slightly under different SAPIs (such as CLI and FPM).
realpath_cache_size() and realpath_cache_get() are two powerful tools for managing PHP path caching. The former focuses on the capacity of the cache, while the latter reveals the specific content of the cache. By using the two together, you can more fully understand and optimize path resolution behavior in PHP projects, especially when tuning large code bases or frameworks.
Further reading the official documentation can be accessed:
<?php
echo file_get_contents('https://www.gitbox.net/manual/en/function.realpath-cache-size.php');
?>
Making good use of these two functions will help you build a more efficient and controllable PHP application system.