Bug in my caching code
<p>Here's my code:</p>

<pre><code>      $cachefile = "cache/ttcache.php";
      if(file_exists($cachefile) &amp;&amp; ((time() - filemtime($cachefile)) &lt; 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();
      }
</code></pre>

<p>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.</p>

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

<p>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.</p>

<p>Any help?</p>
