快捷搜索:   服务器  安全  linux 安全  MYSQL  dedecms

一个强大的PHP缓存类(6)


                    <br />Or choose a supported cache from list:'.$this->getAvailableCache(), E_USER_ERROR);
    }
}
?>
 

基于文件的缓存处理类:

<?php
/*
 * Name:    fileCache
 * URL:     http:/www.admpub.com/
 * Version: v1.1
 * Date:    21/10/2010
 * Author:  Chema Garrido
 * License: GPL v3
 * Notes:   fileCache class, caches variables in standalone files if value is too long or uses unique file for small ones
 */
 
/////////////////////class file cache
 
class fileCache {
    private $cache_path;//path for the cache
    private $cache_expire;//seconds that the cache expires
    private $application=array();//application object like in ASP
    private $application_file;//file for the application object
    private $application_write=false;//if application write is true means there was changes and we need to write the app file
    private $debug=false; //no debug by default
    private $log=array();//log for the debug system
    private $start_time=0;//application start time
    private static $content_size=64;//this is the max size can be used in APP cache if bigger writes independent file
    private static $instance;//Instance of this class
        
    // Always returns only one instance
    public static function GetInstance($exp_time=3600,$path='cache/'){
        if (!isset(self::$instance)){//doesn't exists the isntance
             self::$instance = new self($exp_time,$path);//goes to the constructor
        }
        return self::$instance;
    }
    
    //cache constructor, optional expiring time and cache path
    private function __construct($exp_time,$path){
        $this->start_time=microtime(true);//time starts
        $this->cache_expire=$exp_time;
        if ( ! is_writable($path) ) trigger_error('Path not writable:'.$path);
        else $this->cache_path=$path;
        $this->APP_start();//starting application cache
    }
    
    public function __destruct() {
        $this->addLog('destruct');
        $this->APP_write();//on destruct we write if needed
        $this->returnDebug();
    }
    
    // Prevent users to clone the instance
    public function __clone(){
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
    
    //deletes cache from folder
    public function deleteCache($older_than=''){
        $this->addLog('delete cache');
        if (!is_numeric($older_than)) $older_than=0;
        $files = scandir($this->cache_path);
        foreach($files as $file){          
            if (strlen($file)>2 && time() > (filemtime($this->cache_path.$file) + $older_than) ) {
                unlink($this->cache_path.$file);//echo "<br />-".$file;
                $this->addLog('delete cache file:'.$this->cache_path.$file);
            }
        }
        

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论