9

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)?

Charles
  • 50,943
  • 13
  • 104
  • 142
Giona
  • 20,734
  • 4
  • 56
  • 68
  • 1
    Is there a reason you're using `md5(filemtime('screen.css'))` instead of just `filemtime('screen.css')` -- A 128-bit hash of a 32-bit number is still only as good as the underlying 32-bit number. – jedwards Nov 28 '12 at 15:50
  • @jedwards Just for the coolness of having digits AND letters ;-) but you're right, performance-wise – Giona Nov 28 '12 at 15:55
  • you could always do something like `dechex(filemtime('screen.css'))` or even `str_pad(dechex(filemtime('screen.css')), 8, '0', STR_PAD_LEFT)` -- just an idea – jedwards Nov 28 '12 at 16:11
  • 1
    @jedwards dechex seems to output really similar strings: 50b149a2, 50b152ba, 50b51803 ... while "the Firefox disk cache hash functions can generate collisions for URLs that differ only slightly, namely only on 8-character boundaries" ( from [Google best-practices](https://developers.google.com/speed/docs/best-practices/caching) ) ... so i need a longer, varied string – Giona Nov 28 '12 at 16:34
  • Ah, fair enough. Interesting link too -- thanks. – jedwards Nov 28 '12 at 16:35
  • Giona, the Google link has no reference to that now. I wonder if that Firefox bug resolved itself a long time ago? – Simon East Aug 17 '17 at 23:19

3 Answers3

2

$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.

Simon
  • 37,815
  • 2
  • 34
  • 27
  • 1
    Thanks a lot. Do you have any idea why is the filename parameter "only used if clear_realpath_cache is TRUE", but it "clears the realpath **AND** the stat cache for a specific filename only"...? – Giona Nov 28 '12 at 15:43
2

I cannot give answer directly.

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

pensz
  • 1,871
  • 1
  • 13
  • 18
  • 3
    I don't know about this. The two snippets do very different things. The second takes a hash of a 32-bit integer. The first takes a the hash of an entire file. The first is almost necessarily more work. (Although I don't see why `filemtime('screen.css')` isn't sufficient) – jedwards Nov 28 '12 at 15:46
2

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.

jedwards
  • 29,432
  • 3
  • 65
  • 92
  • 2
    Thanks for your reply. Uhm, then should i use a "general" `clearstatcache()` or `clearstatcache(true,anyfileneeded)` ? – Giona Nov 28 '12 at 15:46
  • Most likely, I would use the general `clearstatcache()` function. The only exception would be if I were doing many more stat functions that I *did* want cached. – jedwards Nov 28 '12 at 15:49