Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • When exactly is a path cached, and under what criteria might it not be cached?

  • How is it cached? In memory, on the file system, something else? If on the file system, where is the file?

  • Are caches per request? i.e. are the multiple caches, or just one canonical realpath cache?

    I've noticed that if you dump the cache var_dump(realpath_cache_get()) and keep, refreshing the page, Ctrl+F5, the cached dump will sometimes have a different output??? What is happening here?

  • How and when is a cache cleared/cleaned? A background process, a garbage collector of some sort. If for example it's a garbage collector: When is it run, under what criteria is it run? Is it per request, random per request for example? I don't know, I'm just spitballing here.

    Note: You seem to be able to clear the cache manually by calling clearstatcache(true).

The realpath_* functions

  • realpath
  • realpath_cache_get
  • realpath_cache_size
  • clearstatcache

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.

like image 298
Gerard Roche Avatar asked Mar 17 '26 10:03

Gerard Roche


1 Answers

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.

like image 137
Pantomedia Avatar answered Mar 20 '26 00:03

Pantomedia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!