document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2.  
  3. /*
  4. ------------------------------------------------------
  5. SIMPLE PAGE CACHING ENGINE
  6. Author: Manish Raj
  7. Website: http://technoslab.blogspot.com
  8. ------------------------------------------------------
  9. Table structure for cache.pages
  10. ------------------------------------------------------
  11. CREATE TABLE `pages` (
  12. `pagehash` varchar(32) DEFAULT NULL,
  13. `page` blob,
  14. `cachetime` bigint(21) DEFAULT NULL
  15. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  16. ------------------------------------------------------
  17. Usage:
  18. ------------------------------------------------------
  19. <?php
  20. require(\'./class.cache.php\');
  21. $cache = new cache(\'localhost\',\'root\',\'\',\'cache\',3306,20);
  22. ?>
  23. ------------------------------------------------------
  24. */
  25.  
  26. class cache
  27. {
  28.     private $dbhost, $dbuser, $dbpass, $dbname, $dbport ;
  29.     private $lifespan = 60 ;
  30.     private $dbhandle = null ;
  31.     private $pagehash = \'\' ;
  32.     private $page = \'\' ;
  33.     private $save = false ;
  34.  
  35.     // Constructor
  36.     function __construct( $dbhost, $dbuser, $dbpass, $dbname, $dbport = 3306, $lifespan = 60 )
  37.     {
  38.         $this->dbhost = $dbhost ;
  39.         $this->dbuser = $dbuser ;
  40.         $this->dbpass = $dbpass ;
  41.         $this->dbname = $dbname ;
  42.         $this->dbport = $dbport ;
  43.         $this->lifespan = $lifespan ;
  44.         $this->connect() ;
  45.         $this->render() ;
  46.     }
  47.  
  48.     // Connect to database
  49.     function connect()
  50.     {
  51.         $this->dbhandle = @new mysqli( $this->dbhost, $this->dbuser, $this->dbpass, $this->dbname, $this->dbport ) ;
  52.         if ( ! empty( $this->dbhandle->connect_error ) )
  53.         {
  54.             exit( \'MySQL connection error.\' ) ;
  55.         }
  56.     }
  57.  
  58.     // Retrive current page\'s md5 hash
  59.     function current()
  60.     {
  61.         return md5( implode( \'\', $_GET ) . implode( \'\', $_POST ) . $_SERVER[\'REQUEST_URI\'] ) ;
  62.     }
  63.  
  64.     function render()
  65.     {
  66.         $current = $this->current() ;
  67.         $lifespan = $_SERVER[\'REQUEST_TIME\'] - $this->lifespan ;
  68.         $check = $this->dbhandle->query( \'SELECT page FROM `pages` WHERE pagehash = "\' . $current . \'" AND cachetime > \' . $lifespan ) ;
  69.         if ( $check->num_rows == 1 )
  70.         {
  71.             $contents = $check->fetch_object() ;
  72.             echo $contents->page ;
  73.             $this->delete() ;
  74.             exit ;
  75.         }
  76.         else
  77.         {
  78.             ob_start() ;
  79.             $this->save = true ;
  80.             $this->pagehash = $current ;
  81.         }
  82.     }
  83.  
  84.     // Save to database
  85.     function save( $pagehash, $page )
  86.     {
  87.         $this->dbhandle->query( \'INSERT INTO `pages` (pagehash,page,cachetime) VALUES ("\' . $pagehash . \'","\' . $this->dbhandle->escape_string( $page ) . \'",\' . $_SERVER[\'REQUEST_TIME\'] . \')\' ) ;
  88.     }
  89.  
  90.     // Delete old cached pages
  91.     function delete()
  92.     {
  93.         $lifespan = $_SERVER[\'REQUEST_TIME\'] - $this->lifespan ;
  94.         $this->dbhandle->query( \'DELETE FROM `pages` WHERE cachetime < \' . $lifespan ) ;
  95.     }
  96.  
  97.     // Destructor
  98.     function __destruct()
  99.     {
  100.         if ( $this->save == true )
  101.         {
  102.             $this->page = ob_get_contents() ;
  103.             ob_end_clean() ;
  104.             $this->save( $this->pagehash, $this->page ) ;
  105.             echo $this->page ;
  106.  
  107.         }
  108.         if ( $this->dbhandle != null )
  109.         {
  110.             $this->dbhandle->close() ;
  111.         }
  112.     }
  113. }
  114.  
  115. ?>
');