I just modified index.php to cache results for 5 minutes, just to make results faster. (This works on my system because I don't typically update entries faster than once a week, and one every 5 minutes is good enough for the seldom-changing content.)
This cheap change requires four steps:
1. Create a cache directory under your blog. I used mkdir 'blog/fastcache/'
2. Add in code that checks the cache for already generated content. If it is less than 5 minutes, then return the content. Otherwise, generate the output into a buffer. Put this at the start of index.php:
Code: Select all
global $NAKCache;
$NAKCache=false;
if ($_SERVER['REQUEST_METHOD']=='GET')
{
if ($_SERVER['REQUEST_URI']=="/blog/index.php?/feeds/index.rss2") { $NAKCache="fastcache/feeds_index.rss2"; }
elseif ($_SERVER['REQUEST_URI']=="/blog/index.php") { $NAKCache="fastcache/index.php"; }
elseif ($_SERVER['REQUEST_URI']=="/blog/") { $NAKCache="fastcache/index.php"; }
}
if ($NAKCache) // If using hacked cache
{
if (is_file($NAKCache) && (time()-filemtime($NAKCache) < 5*60))
{
readfile($NAKCache);
return;
}
ob_start();
}
3. At the end of index.php, add the closing/write-cache code:
Code: Select all
NAKexit:
if ($NAKCache) // Save to hacked cache
{
$Out=ob_get_clean();
if (substr_count($Out,"\n") > 20) { file_put_contents($NAKCache,$Out,LOCK_EX); }
elseif (is_file($NAKCache)) { $Out = file_get_contents($NAKCache); }
echo $Out;
}
Also, if the output is shorter than 20 lines, then I assume there was a bug somewhere. This won't cache bugs.
4. index.php has a couple of 'exit;' calls. That's bad because RSS code won't reach my cache write code. Change every 'exit;' to 'goto NAKexit;'.
That's it. This little code change dramatically sped up the server since it isn't doing a database query each time. And it will auto-refresh within 5 minutes.