I just instituted caching on a site of mine because it’s traffic was jumping and, being on a shared host, I want to avoid trouble as long as possible. It is a “stats” site and does a lot of DB calls.
I created my own functions… one for checking the cache and returning the content or ‘false’. (So I can exit the code if cache is valid.) Another function for saving to cache.
For now I basically made the code so that if the current date and hour do not match the cache file time… it checks the DB and saves a new copy to the cache.
However, I need the cache file to be called and not be updated until 15 minutes after the hour. (Because the data in the DB is being updated up to 10 or so minutes after the hour.)
So, from x:14 to (x+1):14 I need to cache a new file OR pull existing cache file if between that time frame.
I can’t quite wrap my head around the time testing portion. I have thought of a couple paths, but they seem a bit convoluted and I can’t quite get them to come out onto the screen in a reliable way.
I you can think of the a good reliable way to implement the time testing, please feel free to help me out.
Also, for the cache retrieval, I am using the following…
if (TIME TESTING)
{
$results = file_get_contents($cache_file);
return $results;
}
else
{return false;}
This site could have as many as 20 (or higher) cached bits of data being pulled in on one page load. Is “file_get_contents” the best way to do this or should I be doing some kind of “include”.
Thanks for any help!
-Michael
Ok, so you want
a file cached for “about an hour”,
and a cache file can only be regenerated during minutes 15-59?
Consider what would happen if you only get requests for a specific resource during the minutes 1-14. The cache file could potentially get very old and stale. The probability of this happening depends on your traffic, but to me it seems unnecessarily lame if it’s important to serve fresh content.
I’m assuming the motivation for this entire time partitioning issue is because you want to make sure content is as fresh as possible, and that it’s safe to update a cache entry at any time and not get corrupted data.
So, normally, you would want to update the cache once per each wall clock hour during minutes 15-59. But also update the cache no matter if it falls too far behind, because it’s better to update the cache to something that will be updated again very soon, than to let it remain stale.
Does this sound like what you want?
What I was saying is… it can be generated any time, but as soon as it hits the First 15 Minutes after the top of each hour, it is forced to update even if just cached the minute before.
So, from 3:15 pm to 4:14 pm…
It caches a new file if the previous cache was before 3:15
OR
Pulls from the cache if already cached from 3:15 to 4:14
So, if it happens to generate a brand new cache file at 4:14 … and then the data is called again it 4:15… it generates a brand new cache file.
I don’t know, maybe I am looking at this the wrong way. Basically, the stat updating scripts run from the top of the hour to about 14 after and I am trying to deal with that in the best way possible.
Let me know if anyone has any ideas.
Thanks,
Michael
Test it
$offset = 60 * 15;
//$now = time() - $offset;
//$cachedTime = filemtime($file) - $offset;
$now = gmmktime(4, 14, 1, 1) - $offset;
$cachedTime = gmmktime(4, 15, 1, 1) - $offset;
$useCache = $now - $cachedTime < 3600
&& gmdate('G', $now) === gmdate('G', $cachedTime);
var_dump($useCache);
The one hour check is in case the difference is a multiple of 24 hours.
file_get_contents() is a good way to go unless the files are large and memory could be an issue. But that’s more the general issue of whether to buffer the output or not. Most likely alternative is readfile().