RE: [PHP-DEV] [Q] Does PHP have a negative cache for file stat operations?
| From: | Eric Stenson | Date: | Fri, 20 Mar 2015 17:18:37 +0000 |
| Subject: | RE: [PHP-DEV] [Q] Does PHP have a negative cache for file stat operations? | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-85309@lists.php.net to get a copy of this message | ||
From: Stanislav Malyshev [mailto:smalyshev@gmail.com]
> > Does the PHP stat cache include negative cache entries? If not, why
> > not?
>
> Negative cache is tricky. Most frequent patterns look like this:
>
> 1. if(file_exists("blah")) { do stuff }
>
> 2. if(!file_exists("blah")) { throw new Exception("no blah!"); } do stuff
>
> 3. if(!file_exists("blah")) { create("blah"); } do stuff
>
> Caching negative even short term would subtly break case 3, in that it
> would force creation of the file every time, thus killing any benefit
> from any caching (usually file creation is much more expensive process).
Yes, and that was my biggest worry about implementing a negative cache.
I'd have to hook all the ways a file could be created in order to correctly
flush entries in the negative cache. And, in the case of a site that doesn't
pathologically try to hit non-existent files, the overhead of a negative
cache is wasted cycles.
> Long term caching would also be big trouble with case 2. Unless there's a
> way to reset the cache and the app is very careful to do it - but file can
> be created by external means and also case 2 would be harder to fix without
> losing the benefits of caching.
Yes, it's the problem of detecting & hooking all the ways a file could be
created, and then flushing negative entries. And, as you've pointed out,
since there's potentially other, non-hookable ways to create files, I'd have
to provide API surface to flush entries out of the cache.
Thank you for the thoughtful response!
--E.