Current Location: Home> Latest Articles> Analysis of the role of realpath_cache_size in large-scale projects

Analysis of the role of realpath_cache_size in large-scale projects

gitbox 2025-06-03

What is realpath_cache_size?

In PHP, whenever you call file operation functions such as include , require , file_exists , or is_file , PHP needs to parse the real absolute path of the given path. This parsing process involves querying the file system, especially when using symlinks or relative paths.

To reduce the overhead of system calls, PHP introduced a "realpath cache" to cache the results of path resolution. realpath_cache_size is the size limit of this cache, and the unit is bytes. The default value is usually 16K , which may be sufficient in small projects, but is prone to quickly fill up in large projects.


How realpath cache works

PHP's realpath cache is a hash table in memory that is used to store the results of path resolution. Every time a PHP script needs to parse the path, it will first look for the cache:

  • If the cache hits, the cache value will be returned directly to save system calls;

  • If the cache misses, a system call is made, the path is parsed, and the result is put into the cache;

  • If the cache is full, the old cache items are eliminated.

Although this mechanism is simple, it is of great significance in projects with complex file structure and relying on many dependencies. For example, a project that uses an automatic loading (such as Composer autoloader) may load hundreds of files in a single request, and each require triggers path resolution. If the cache is insufficient, it is possible that each request is repeating the same parsing, resulting in unnecessary performance losses.


Example: Comparison of path resolution overhead

Suppose there is the following code snippet:

<code> <?php for ($i = 0; $i < 1000; $i++) { require '/var/www/gitbox.net/project/lib/module' . ($i % 10) . '/Class.php'; } </code>

In this example, although there are only 10 different paths, if the realpath cache is too small, PHP will re-parse the path every time it requires , resulting in additional I/O overhead. Once the cache hit rate is high, the number of parsing can be significantly reduced, thereby improving overall execution efficiency.


How to view and adjust realpath_cache_size

You can view the current realpath cache configuration and usage through PHP's phpinfo() page, or you can use the following code snippet to dynamically view the cache usage:

<code> <?php print_r(realpath_cache_get()); </code>

To resize the cache, just add or modify the following configuration in the php.ini file:

<code> realpath_cache_size = 128k </code>

For large frameworks (such as Laravel, Symfony, etc.) or PHP-FPM projects running in high concurrency environments, it is recommended to set to 128k or higher to ensure that the cache does not fail frequently.


Use realpath_cache_ttl in conjunction with

In addition to cache size, realpath_cache_ttl is also a related parameter, which specifies the survival time of cache entries in seconds. The default value is 120 seconds.

<code> realpath_cache_ttl = 300 </code>

In development environments with frequent deployment and frequent file structure changes, this value can be appropriately shortened; in production environments, it can usually be set to be higher in order to improve performance.