Cache class
- /*
- * Cache class cache
- * Author: Duo Noob
- * Creation time: 2006-05-05
- * Example:
- include( "cache.php" );
- $cache = new cache(30);
- $cache->cacheCheck();
- echo date("Y-m-d H:i:s");
- $cache->caching();
- */
- class cache {
- // Cache directory
- var $cacheRoot = "./cache/";
- //Cache update time in seconds, 0 means no caching
- var $cacheLimitTime = 0;
- //Cache file name
- var $cacheFileName = "";
- / /Cache extension
- var $cacheFileExt = "php";
- /*
- * constructor
- * int $cacheLimitTime cache update time
- */
- function cache( $cacheLimitTime ) {
- if( intval( $cacheLimitTime ) )
- $ this->cacheLimitTime = $cacheLimitTime;
- $this->cacheFileName = $this->getCacheFileName();
- ob_start();
- }
- /*
- * Check whether the cache file is within the set update time
- * Return: If it is within the update time, return the file content, otherwise it will return failure
- */
- function cacheCheck(){
- if( file_exists( $this->cacheFileName ) ) {
- $cTime = $this->getFileCreateTime ( $this->cacheFileName );
- if( $cTime + $this->cacheLimitTime > time() ) {
- echo file_get_contents( $this->cacheFileName );
- ob_end_flush();
- exit;
- }
- }
- return false;
- }
- /*
- * Cache files or output static
- * string $staticFileName static file name (including relative path)
- */
- function caching( $staticFileName = "" ){
- if( $this ->cacheFileName ) {
- $cacheContent = ob_get_contents();
- //echo $cacheContent;
- ob_end_flush();
-
- if( $staticFileName ) {
- $this->saveFile( $staticFileName, $cacheContent );
- }
-
- if( $this->cacheLimitTime )
- $this->saveFile( $this->cacheFileName, $cacheContent );
- }
- }
- /*
- * Clear cache files
- * string $fileName specified file name (including functions) or all (all)
- * Return: true if clearing is successful, otherwise false
- */
- function clearCache( $fileName = "all" ) {
- if( $fileName != "all" ) {
- $fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt;
- if( file_exists( $fileName ) ) {
- return @unlink( $fileName );
- }else return false;
- }
- if ( is_dir( $this->cacheRoot ) ) {
- if ( $dir = @opendir( $this->cacheRoot ) ) {
- while ( $file = @readdir( $dir ) ) {
- $check = is_dir( $file );
- if ( !$check )
- @unlink( $this->cacheRoot . $file );
- }
- @closedir( $dir );
- return true;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
- /*
- * Generate a cache file name based on the current dynamic file
- */
- function getCacheFileName() {
- return $this-> cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
- }
- /*
- * Cache file creation time
- * string $fileName cache file name (including relative path) )
- * Return: file generation time in seconds, if the file does not exist, return 0
- */
- function getFileCreateTime( $fileName ) {
- if( ! trim($fileName) ) return 0;
-
- if( file_exists( $fileName ) ) {
- return intval(filemtime( $fileName ));
- }else return 0;
- }
- /*
- * Save the file
- * string $fileName file name (including relative path)
- * string $text file content
- * Return: Returns true on success, false on failure
- */
- function saveFile($fileName, $text) {
- if( ! $fileName || ! $text ) return false;
-
- if( $this->makeDir( dirname( $ fileName ) ) ) {
- if( $fp = fopen( $fileName, "w" ) ) {
- if( @fwrite( $fp, $text ) ) {
- fclose($fp);
- return true;
- }else {
- fclose($fp);
- return false;
- }
- }
- }
- return false;
- }
- /*
- * Continuously create directories
- * string $dir directory string
- * int $mode permission number
- * return : Return true if created successfully or all have been built, otherwise return false
- */
- function makeDir( $dir, $mode = "0777" ) {
- if( ! $dir ) return 0;
- $dir = str_replace( "\ ", "/", $dir );
-
- $mdir = "";
- foreach( explode( "/", $dir ) as $val ) {
- $mdir .= $val."/";
- if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
-
- if( ! file_exists( $mdir ) ) {
- if(!@mkdir( $mdir, $mode )){
- return false;
- }
- }
- }
- return true;
- }
- }
- ?>
-
-
Copy code
|