10

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

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.

Community
  • 1
  • 1
Gerard Roche
  • 6,162
  • 4
  • 43
  • 69

2 Answers2

5

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.

Gerard Roche
  • 6,162
  • 4
  • 43
  • 69
Pantomedia
  • 86
  • 1
  • 2
-2

PHP realpath function test $ path, the path pointing to the target files/folder to check the file exits or not, or we can say equivalent to calling the file_exists ($ path).

if the target file exists and is not a symbolic link (windows under the name “shortcut”) returns the absolute path of the file name does not contain’/./’ or ‘/../’

If the target file is a symbolic link, or does not exist, realpath () returns FALSE.

var_dump (realpath (‘./Test.php’));

If the./Test.php file path can be found, then the output is:

string ‘E: \ Dropbox \ My Dropbox \ code \ php \ test.php’ (length=48)

If the./path is a symbolic link to find test.php , then the output is:

boolean false

If the./test.php can not find the file path, then the output is:

boolean false

if it is running under the Windows platform, the results of the implementation above line of code, because in Windows, both slash (/) and backslash () can be used as directory separator character.

var_dump (realpath (‘. \ Test.php’));

hope the above description of the PHP realpath path to knowledge of the function can be helpful to everyone.

Cache:

The cache is maintained per-thread, so it’s not a silver bullet.

Clearing Cache:

Only the active thread will have its cache cleared, where you may have dozens of threads in total (e.g. instances of php-fpm, or httpd children in prefork mode). The most significant is that when open_basedir is enabled, the PHP realpath cache will be disabled.

Filecache caches files on the local disk of the webserver, but passes all stat() calls on to the dbstatcache. Dbstatcache caches stat information in a database that is accessible by all webservers in the cluster (this is necessary in order to detect if an already cached file is deleted or updated)

Vineet1982
  • 7,730
  • 4
  • 32
  • 67