<?php
class Cache{
var $path;
var $file;
var $expireTime;
var $modifiedTime;
var $extension = ".cache";
function Cache( $path , $expireTime = 120 ){
clearstatcache();
$this->setPath( $path );
$this->setExpireTime( $expireTime );
$this->setFile( $_SERVER['PHP_SELF'] );
$this->setModifiedTime( $this->path . $this->file . $this->extension );
}
function setExtenstion( $extension ){
$this->extension = ( string ) $extension;
}
function getExtension(){
return $this->extension;
}
function setPath( $path ){
// creates the cache directory if not exist
if ( is_dir( $path ) === FALSE ){
mkdir( $path , 0777 );
}
// validates the cache path
$length = ( INT ) strlen( $path ) - 1 ;
if ( $path[$length] != "/" ) {
$path .= "/";
}
$this->path = $path ;
}
function getPath(){
return $this->path;
}
function setFile( $file ) {
$path = pathinfo( $file );
$file = $path['basename'];
$this->file = ( STRING ) $file;
}
function getFile(){
return $this->file;
}
function setExpireTime( $time ) {
$this->expireTime = $time ;
}
function getExpireTime(){
return $this->expireTime;
}
function setModifiedTime( $file ){
if ( file_exists( $file ) ) {
$this->modifiedTime = ( time() - filemtime( $file ) ) ;
}else{
$this->modifiedTime = 0;
}
}
function getModifiedTime(){
return $this->modifiedTime;
}
function read(){
//readfile ( $this->path . $this->file . $this->extension );
print file_get_contents ( $this->path . $this->file . $this->extension );
exit();
}
function save(){
$contents = ob_get_contents();
$file = $this->path . $this->file . $this->extension;
$handle = fopen( $file , "w" );
fputs( $handle , $contents );
fclose( $handle );
/*
ob_end_clean();
print $contents;
*/
}
function isCached(){
global $HTTP_GET_VARS;
$ret = TRUE;
if (
$this->modifiedTime > $this->expireTime ||
!file_exists( $this->path . $this->file . $this->extension ) ||
isset( $_GET['update'] )
) {
ob_start();
$ret = FALSE;
}
return $ret;
}
}
?>
|