This is a PHP question for those smarter than I. I was debating whether to post this on a forum specifically for PHP support, but I'll post it here instead. :)
I've been trying to implement a serverside caching mechanism for my weblog system. The basic setup is currently:
1. In the constructor of the main class which is instantiated on every page, check to see if the request is a GET.
1a. Check to see if there is a cached file for this particular request URI, if there is retrieve it, serve it and then exit the script.
1b. If there isn't a cached file, set a variable notifying the final output method to cache the current URI.
2. The final method is called to output the content - before outputting the content, check to see if the content needs to be cached, if it does, cache it.
It works well, however, in order to clear the cache, I hooked a method in my database class to clear the entire cache directory if there is any SQL executed that could possibly alter a row (UPDATE, INSERT, DELETE). This is what concerns me. This means that whenever a comment/article is posted/edited/deleted, the *entire* cache directory will be cleared via: "foreach(glob('cache/*') as $f) unlink($f);". Is this efficient? My limited knowledge of filesystems tells me that unlink should be efficient because all it does is sever the link between the name and file, and then the data is removed whenever possible. But unfortunately that is my very limited knowledge of the subject.
I could make cache removal very selective, but it seems like more work than its worth. Any tips?