file_exists performance concerns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

file_exists performance concerns

Post by choppsta »

I'm putting together a simple Caching system that uses flat files for storage. This means that every request, pretty much, will involve a call to file_exists() to check for a cache file, even for modules/pages marked as not cachable.

My question is what kind of overhead is there with file_exists(), if any? Will this scale well? Should I not worry about it? Has anyone looked into this, done any tests?

I understand the results of file_exists() are cached (only if a file IS found) by PHP, but is this per request or across the whole server?

p.s. Please don't reply saying "use xyz caching system" or "caching is rubbish", my question is about the efficiency of the file_exists() function.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

My question is what kind of overhead is there with file_exists(), if any? Will this scale well? Should I not worry about it? Has anyone looked into this, done any tests?
I would suggest you try some tests. Should be easy to itterate checking if files exist, because this sounds like it depends a lot on the OS and particular system.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

clearstatcache

This value is only cached for the lifetime of a single request.

PHP is stateless. so when execution is done any information held in a variable assignment, defined or cached reference is removed at the end of the script process. So caching is per script process. The only time a cached reference like in the case of file_exists() would be available to another process is if another process is started and the other process has not completed, then if file_exists was called in the new process, the new process would have access to the boolean value associated with the file being tested in the file_exists function within the new process. So caching really has no real benefit other than a script that may call file_exists on the same file in the current process!

You can create a object container, that holds references to objects like file objects, that would always be available to all scripts across the server. But to do that you have to hack the core, I have some code to do this if you want it. In Windows you can create a shell object and load that object into PHP via COM, that gives complete access to that object and all it's properties across all script calls, in this case the object is loaded into the server, so it exists until the server is shut down or restarted!

Other than that, file_exists() is a system call so it is an a expensive resource. If your using it a lot in your script then maybe it would be better to cache your directories into strings and use substr_count() or use an array and just check if the file is in the array!


me!
Post Reply