AKor
AKor

Reputation: 8892

Bug in my caching code

Here's my code:

      $cachefile = "cache/ttcache.php";
      if(file_exists($cachefile) && ((time() - filemtime($cachefile)) < 900))
      {
            include($cachefile);
      }
      else
      {
            ob_start();

            /*resource-intensive loop that outputs 
            a listing of the top tags used on the website*/

            $fp = fopen($cachefile, 'w');
            fwrite($fp, ob_get_contents());
            fflush($fp);
            fclose($fp);
            ob_end_flush();
      }

This code seemed like it worked fine at first sight, but I found a bug, and I can't figure out how to solve it. Basically, it seems that after I leave the page alone for a period of time, the cache file empties (either that, or when I refresh the page, it clears the cache file, rendering it blank). Then the conditional sees the now-blank cache file, sees its age as less than 900 seconds, and pulls the blank cache file's contents in place of re-running the loop and refilling the cache.

I catted the cache file in the command line and saw that it is indeed blank when this problem exists.

I tried setting it to 60 seconds to replicate this problem more often and hopefully get to the bottom of it, but it doesn't seem to replicate if I am looking for it, only when I leave the page and come back after a while.

Any help?

Upvotes: 0

Views: 77

Answers (2)

Alexander Blair
Alexander Blair

Reputation: 106

In the caching routines that I write, I almost always check the filesize, as I want to make sure I'm not spewing blank data, because I rely on a bash script to clear out the cache.

if(file_exists($cachefile) && (filesize($cachefile) > 1024) && ((time() - filemtime($cachefile)) < 900))

This assumes that your outputted cache file is > 1024 bytes, which, usually it will be if it's anything relatively large. Adding a lock file would be useful as well, as noted in the comments above to avoid multiple processes trying to write to the same lock file.

Upvotes: 1

preinheimer
preinheimer

Reputation: 3722

you can double check the file size with the filesize() function, if it's too small, act as if the cache was old.

if there's no PHP in the file, you may want to either use readfile() for performance reasons to just spit the file back out to the end user.

Upvotes: 0

Related Questions