Using PEAR to cache cafelog/b2

Hacking cafelog/b2 can be a pain. I tried to add some caching based on PEAR::Cache while adding it to the code of b2. There was no reasonable way to do so within an acceptable amount of time. But the pages went slow on the blog and there was something to do about it.

So I think there is a way to handle it; at least it looks like. The caching now done is based on the idea, that I take an application and say something like \”start caching\” at the very beginning of the script and \”stop it\” at the very end. A way to control such mechanism is done by the Apache directive php_value, where one can reconfigure the Apache server configuration. Therefore the parameters auto_prepend_file and auto_append_file have been used. They allow exactly a handling of such a start-stop mechanism. What follows is some lines of code of PHP, split up into two files, the prepend.php and the append.php.

This solution is probably suboptimal but it improved the accessability of the blog posts at least subjectively but hopefully not only. Any improvements are welcome.

Lots of good discussions about caching can be found on Simons blog [1].

01  <?php

02  
// $Id: prepend.php,v 1.1 2003-09-11 01:31:17+02 urs Exp urs $

03  

04  /**

05  * Quick & dirty way of caching a blog.

06  * Put this into your .htaccess file:

07  *   php_value auto_prepend_file /home/example.com/htdocs/prepend.php

08  *   php_value auto_append_file  /home/example.com/htdocs/append.php

09  *

10  * References:

11  * – http://ch.php.net/manual/de/ref.outcontrol.php

12  * – http://www.onlamp.com/lpt/a/1227

13  * – http://www.ulf-wendel.de/php/show_source.php?file=cache_dem_file

14  * – http://iamcal.com/publish/articles/php/processing_html/

15  */

16  

17  // Include the package

18  
require_once \’Cache/Output.php\’;

19  

20  
// Set some variables

21  $cache_path = array(

22                    \’cache_dir\’       =>
\”/home/example.com/cache/\”,  

23                    
\’filename_prefix\’ => \”cache_\”

24                
);

25  

26  
// Create a Cache object

27  
$objCache = &new Cache_Output(\’file\’, $cache_path );

28  

29  
// Compute unique cache identifier for the page

30  $cache_params = array(

31                      
\’url\’       => $_SERVER[\’REQUEST_URI\’],

32                      
\’post\’      => $_POST,

33                      \’cookies\’   => $_COOKIE

34                  
);

35  

36  
$urltest = parse_url(\”http://\”.$_SERVER[\’HTTP_HOST\’].$_SERVER[\’REQUEST_URI\’] );

37  if( preg_match(\’#([cmp]+)([0-9]{1,}).html#\’, $urltest[\’path\’] , $match )  

38      OR (
trim($urltest[\’path\’], \”/\”) == \”blog\”) ) {

39      

40      
// Set an ID for this cache

41      
$cache_id = $objCache->generateID($cache_params );

42  

43      
// Query the cache

44      if ( $content = $objCache->start($cache_id )  ) {

45      

46          
// Cache Hit

47          
echo $content;

48          exit(\”[Cache hit]\”);

49      }

50  }

51  

52  
?>

 

01  <?php

02  
// $Id: append.php,v 1.1 2003-09-11 01:29:08+02 urs Exp urs $

03  

04  /**

05  * Test if there is any cached data. If not obtain it,

06  * save and display it.

07  *

08  */

09  
if( strlen($objCache->start($cache_id)) < 1 ) {

10      // This should not be here either, but it is obviously required.

11      // Tell me if You know any better…

12      
ob_end_clean();

13  

14      
// Cache pages starting with pXXX.html, cXXX.html and mXXX.html. The main

15      // page http://example.com/blog/ is being cached using a parse_url trick.

16      
if( preg_match(\’#([cmp]+)([0-9]{1,}).html#\’, $urltest[\’path\’] , $match )

17              OR ( trim($urltest[\’path\’], \”/\”) == \”blog\”) ) {

18          
// Store page into cache

19          
echo $objCache->end(3600);

20      }

21  }

22  
?>

[1] http://simon.incutio.com/archive/2003/06/21/

Leave a Reply