Code: Select all
<?php
if (empty ($_POST[serendipity])){
// Settings
$cachetime = 3600; // Seconds to cache files for
echo $_POST[newcache];
if ($_POST[newcache]=="on") {
$cachetime = 5;
}
$cachedir = 'cache/'; // Directory to cache files in (keep outside web root)
$cacheext = 'html'; // Extension to give cached files (usually cache, htm, txt)
// Ignore List
$ignore_list = array(
'faq.html',
'music.html'
);
// Script
$cachepage = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
$cachefile = $cachedir . md5($cachepage) . '.' . $cacheext; // Cache file to either load or create
$ignore_page = false;
for ($i = 0; $i < count($ignore_list); $i++) {
$ignore_page = (strpos($cachepage, $ignore_list[$i]) !== false) ? true : $ignore_page;
}
$cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
@clearstatcache();
// Show file from cache if still valid
if (time() - $cachetime < $cachefile_created) {
//ob_start('ob_gzhandler');
@readfile($cachefile);
//ob_end_flush();
exit();
}
// If we're still here, we need to generate a cache file
ob_start();
} else {
echo "Page not cached this time.";
}
?>Code: Select all
<?php
$cachedir = 'cache/'; // Directory to cache files in (keep outside web root)
$cacheext = 'html'; // Extension to give cached files (usually cache, htm, txt)
$cachepage = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
$cachefile = $cachedir . md5($cachepage) . '.' . $cacheext; // Cache file to either load or create
// Now the script has run, generate a new cache file
$fp = @fopen($cachefile, 'w');
// save the contents of output buffer to the file
@fwrite($fp, ob_get_contents());
@fclose($fp);
ob_end_flush();
?>Code: Select all
<ul class="serendipitySideBarMenu">
<li><a href="<?php echo $serendipity['baseURL'];?>"><?php echo BACK_TO_BLOG; ?></a></li>
<li><a href="serendipity_admin.php?serendipity[adminModule]=logout"><?php echo LOGOUT; ?></a><br /><form method = "post" action = "<?php echo $serendipity['baseURL'];?>">
<input type="checkbox" id="cachenew" name="newcache" checked="checked" />
Cache new?<br />
<input type="submit" value="GO!" />
</form></li>
</ul>When a comment is previewed, it is not cached - corrections are shown when a visitor hit the "Preview" button again.
When a comment is submitted, it is shown after the "Submit" button is hit, but will only show as part of the comments after an hour because the other pages are still cached until the hour expires.
If in admin panel and after a new post is saved, and the box is checked and the "GO!" button hit, the new post will show immediately as the cache time is then only 5 seconds.
I have tested it, and it works, and my host stopped complaining about the server load on the MySQL server. (Actually host is seriously over selling)
I am not very good with PHP coding etc. and created the code with the help of a web tutorial and a beginner's PHP book.