My cache script

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
Yoggi
Regular
Posts: 8
Joined: Fri Oct 28, 2005 10:51 am

My cache script

Post by Yoggi »

I have added the following at the beginning of index.php:

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.";
}
?>
and the following at the end of index.php:

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(); 

?>
and changed the code in serendipity_admin.php to the following:

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>
The pages are then cached for 1 hour.
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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: My cache script

Post by garvinhicking »

Why didn't use use the "serendipity_event_cachesimple" plugin? *g* It actually does what you want, but without hacking, and with paying attention to some auto-pruning. :-)

But your code looks good to me. I am sure it works well for you. :-)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Yoggi
Regular
Posts: 8
Joined: Fri Oct 28, 2005 10:51 am

Post by Yoggi »

LOL. I should have checked the available plugins...
At least I learned something.
Post Reply