phpcachingrealpath

How does PHP realpath cache work under the hood?


I have been unable to figure out what the realpath cache is actually doing.

Here are some of the questions I have:

The realpath_* functions

The Configuration Options

realpath_cache_size "16K" PHP_INI_SYSTEM Available since PHP 5.1.0.

realpath_cache_ttl "120" PHP_INI_SYSTEM Available since PHP 5.1.0.

(from the manual)

realpath_cache_size integer Determines the size of the realpath cache to be used by PHP. This value should be increased on systems where PHP opens many files, to reflect the quantity of the file operations performed.

realpath_cache_ttl integer Duration of time (in seconds) for which to cache realpath information for a given file or directory. For systems with rarely changing files, consider increasing the value.


Solution

  • Realpath cache is populated when realpath() is called.

    Subsequent calls to realpath() for the same file will be extracted quickly from realpath cache.

    Realpath cache is not the same cache used by common filesystem functions (stat, file_exists, ...).

    Realpath cache is per process and its entries remain alive for the duration specified inside the realpath_cache_ttl php.ini setting.