Php clearstatcache () description of performance and parameters

I use filemtime to print external resources in html, for example:

 <link rel="stylesheet" href="screen-<?=md5(filemtime('screen.css'));?>.css"> 

I noticed a significant delay between the effective update and the timestamp returned by filemtime , so I added clearstatcache() at the top, which seems to solve the problem. But according to php manual :

you only need to call clearstatcache () if you are performing several operations with the same name and require a specific file that cannot be cached.

So I wonder if I use it correctly.

Also, I am concerned about the performance of completely clearing the cache on every call. Can someone tell me if this could cause a significant slowdown on the server?


clearstatcache also accepts two additional parameters, but I'm not sure about their meaning:

clear_realpath_cache Clear the cache of the real path or not.

file_name Clear realpath and stat cache for a specific file name. only; used only if clear_realpath_cache is set to TRUE.

I do not understand what the "realpath cache" is, and I could not find any information about it. Does it make sense to call clearstatcache as follows:

 clearstatcache(true,'/path/to/screen.css'); 

with the intention of clearing only the information associated with this particular file (and therefore reducing the "impact" of clearstatcache )?

+7
source share
3 answers

It seems that you are using the function correctly. If you do not use other stat functions (as indicated in the document) that you would prefer to cache, I do not know the reason why this will lead to a significant slowdown.

When you include('somefile') , somefile may be in different places as defined by your include_path , cwd, etc. The realpath cache simply eliminates the need to re-search these locations.

For your use, your code looks fine.

+1
source

$ clear_realpath_cache refers to calls to the realpath function, the results of which are also cached. This should not affect your calls in filemtime.

+2
source

I can not give an answer directly.

But I suggest using md5_file('screen.css') instead of md5(filemtime('screen.css')) .

+2
source

All Articles