Here's an example from the last couple of days which is fully my own work, so I can share!
I was analysing a big data flow - yesterday's web log file in this case - and it was rather harsh on the server. No big shock about that - the log file's around 40Mbytes per day and I didn't have it in anything other than a normal text file. But it was rather silly to analyse it every time as the data only changes once every 24 hours.
Solution - let it run slowly the first time it's used after the log files are cycled, but then store the results in a cache and replay it from there each time. A user won't notice the first hit being slow if he can then call up other variants of the page at the blink of an eye!
You can try the code
here (separate window) and chances are it will be very fast for you, as someone else will probably have been there recently. If not, the first time it will be slow then it will be fast. Full source code is available
here.
Salient points:
1.
if (filemtime("ac_20090522") > @filemtime("cache_0.txt")) {
"is the log file newer that any cache." In the live demo, I have changed the code slightly to pick up yesterday's data every time. Note the "@" to suppress the warning message from filemtime if the cache file doesn't exit
2.
$fho = fopen("cache_0.txt","w");
fputs($fho,"$lines<%%>$result1<%%>$result2<%%>$result3\n");
fclose($fho);
}
# ------------- End of Code to set up cache --------------
"save the results to the cache". All the work of analysis has been done and the data for all the reports that might be called up for the next day is dumped into a file and then ...
3.
$parts = explode("<%%>",file_get_contents("cache_0.txt"));
"read back all the results from the cache". Note that I have NOT added a conditional - even if the cache file has just been generated, I let my code re-read it. Really I did that for testing purposes, but there's no harm in calling it back as - remember - reading the cache is FAST.
4.
Don't forget to make the cache file writable by the PHP!
(written 2009-05-24, updated 2009-05-26)
Associated topics are indexed under
H307 - PHP - Web2 and caching [3458] On this day ... one PHP script with three uses - (2011-09-26)
[3186] How to add a customised twitter feed to your site - (2011-02-27)
[3094] Setting your user_agent in PHP - telling back servers who you are - (2010-12-18)
[3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
[2545] Scraping content for your own page via PHP - (2009-12-21)
[2321] Uploading and Downloading files - changing names (Perl and PHP) - (2009-08-04)
[1995] Automated server heartbeat and health check - (2009-01-16)
[1926] Flash (client) to PHP (server) - example - (2008-12-06)
[1814] Javascript/HTML example, dynamic server monitor - (2008-09-28)
[1813] Ajax - going Asyncronous and what it means - (2008-09-28)
[1812] Starting Ajax - easy example of browser calling up server data - (2008-09-27)
[1733] memcached - overview, installation, example of use in PHP - (2008-08-02)
[1647] Exchange Rates - PHP with your prices in your users currency - (2008-05-19)
[1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)
Some other Articles
England, Scotland, Northern IrelandImproving the structure of your early PHP programsOn the road, at a Service area thats a cut above the othersNew Example - cacheing results in PHP for faster loadingA two day gap - first time in five years?Melksham Business Map - Chamber of Commerce, Tourist Information and Town CouncilExcellent product, excruciating customer service. 3 Mobile BroadbandCopy writing - allowing for the cutRT @brento - a valuable source for the twitter newbieHow you are (re)presented at an exhibition