Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- ------------------------------------------------------
- SIMPLE PAGE CACHING ENGINE
- Author: Manish Raj
- Website: http://technoslab.blogspot.com
- ------------------------------------------------------
- Table structure for cache.pages
- ------------------------------------------------------
- CREATE TABLE `pages` (
- `pagehash` varchar(32) DEFAULT NULL,
- `page` blob,
- `cachetime` bigint(21) DEFAULT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1
- ------------------------------------------------------
- Usage:
- ------------------------------------------------------
- <?php
- require('./class.cache.php');
- $cache = new cache('localhost','root','','cache',3306,20);
- ?>
- ------------------------------------------------------
- */
- class cache
- {
- private $dbhost, $dbuser, $dbpass, $dbname, $dbport ;
- private $lifespan = 60 ;
- private $dbhandle = null ;
- private $pagehash = '' ;
- private $page = '' ;
- private $save = false ;
- // Constructor
- function __construct( $dbhost, $dbuser, $dbpass, $dbname, $dbport = 3306, $lifespan = 60 )
- {
- $this->dbhost = $dbhost ;
- $this->dbuser = $dbuser ;
- $this->dbpass = $dbpass ;
- $this->dbname = $dbname ;
- $this->dbport = $dbport ;
- $this->lifespan = $lifespan ;
- $this->connect() ;
- $this->render() ;
- }
- // Connect to database
- function connect()
- {
- $this->dbhandle = @new mysqli( $this->dbhost, $this->dbuser, $this->dbpass, $this->dbname, $this->dbport ) ;
- if ( ! empty( $this->dbhandle->connect_error ) )
- {
- exit( 'MySQL connection error.' ) ;
- }
- }
- // Retrive current page's md5 hash
- function current()
- {
- return md5( implode( '', $_GET ) . implode( '', $_POST ) . $_SERVER['REQUEST_URI'] ) ;
- }
- function render()
- {
- $current = $this->current() ;
- $lifespan = $_SERVER['REQUEST_TIME'] - $this->lifespan ;
- $check = $this->dbhandle->query( 'SELECT page FROM `pages` WHERE pagehash = "' . $current . '" AND cachetime > ' . $lifespan ) ;
- if ( $check->num_rows == 1 )
- {
- $contents = $check->fetch_object() ;
- echo $contents->page ;
- $this->delete() ;
- exit ;
- }
- else
- {
- ob_start() ;
- $this->save = true ;
- $this->pagehash = $current ;
- }
- }
- // Save to database
- function save( $pagehash, $page )
- {
- $this->dbhandle->query( 'INSERT INTO `pages` (pagehash,page,cachetime) VALUES ("' . $pagehash . '","' . $this->dbhandle->escape_string( $page ) . '",' . $_SERVER['REQUEST_TIME'] . ')' ) ;
- }
- // Delete old cached pages
- function delete()
- {
- $lifespan = $_SERVER['REQUEST_TIME'] - $this->lifespan ;
- $this->dbhandle->query( 'DELETE FROM `pages` WHERE cachetime < ' . $lifespan ) ;
- }
- // Destructor
- function __destruct()
- {
- if ( $this->save == true )
- {
- $this->page = ob_get_contents() ;
- ob_end_clean() ;
- $this->save( $this->pagehash, $this->page ) ;
- echo $this->page ;
- }
- if ( $this->dbhandle != null )
- {
- $this->dbhandle->close() ;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment