Home » Php » stat – php clearstatcache() performance and parameters explanation

stat – php clearstatcache() performance and parameters explanation

Posted by: admin July 12, 2020 Leave a comment

Questions:

i’m using filemtime for fingerprinting external resources in html, like:

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

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

you only need to call clearstatcache() if you are performing multiple
operations on the same filename and require the information about that
particular file to not be cached.

So i’m wondering if i’m using it correctly.

Also, i’m concerned about the performance of fully clearing the cache on every call. Can anyone tell me if it can cause a significant slowdown in the server?


clearstatcache also accepts two additional parameters, but i’m unsure of their meaning:

clear_realpath_cache Whether to clear the realpath cache or not.

filename Clear the realpath and the stat cache for a specific filename
only; only used if clear_realpath_cache is TRUE.

I don’t get what “realpath cache” means, and i couldn’t find any information about it. Does it make any sense to call clearstatcache this way:

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

with the intent to clear only information related to that specific file (and therefore reduce the “impact” of clearstatcache)?

How to&Answers:

It seems like you are using the function correctly. Unless you’re using other stat functions (as listed in the doc) that you would prefer cached, I don’t know of a reason it would cause a significant slowdown.

When you include('somefile'), somefile could be in a number of different locations, as determined by things like your include_path, cwd, etc. The realpath cache just eliminates the need to repeatedly search these locations.

For your use, your code seems fine.

Answer:

$clear_realpath_cache relates to calls to the realpath function, the results of which are also cached.
This should have no impact on your calls to filemtime.

Answer:

I cannot give answer directly.

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