null,                  //数据缓存目录        'life_time' => 7200,                  //缓存时间        'page_dir' => null,                   //文本缓存目录        'cache_prefix' => 'cache_'        //缓存前缀    );    private function __construct(){}       //创建__clone方法防止对象被复制克隆    private function __clone(){}       /**     * 取缓存对象,如果存在直接返回,如果不存在实例化本身     * @return object cache     */    public static function getinstance(){               if(! self::$_instance){                   self::$_instance = new self();        }               return self::$_instance;    }           /**     * 设置缓存参数集     * @param array $options 要设置的缓存参数集     */    public function setoptions($options = array()){           while (list($name, $value) = each($options)) {            $this->setoption($name, $value);        }    }       /**     * 取得当前缓存参数,如果$name为空返回全部参数,否则返回该参数值     * @param string $name 要返回的参数名称     * @return string or array $option;     */    public function getoption($name = null){           if(null === $name)            return $this->_options;           if (!is_string($name)) {            throwexception(不正确的参数名称 : $name);        }               if (array_key_exists($name, $this->_options)){            return $this->_options[$name];        }    }       /**     * 设置缓存参数     * @param array $options 要设置的缓存参数     */    protected function setoption($name, $value){           if (!is_string($name)) {            throwexception(不正确的参数名称 : $name);        }        $name = strtolower($name);        if (array_key_exists($name, $this->getoption())){            $this->_options[$name] = $value;        }               if ($this->_options['cache_dir'] === null) {            $this->setoption('cache_dir', $this->gettmpdir() . directory_separator);        }               if ($this->_options['page_dir'] === null) {            $this->setoption('page_dir', $this->gettmpdir() . directory_separator);        }    }       /**     * 读取数据缓存,如果不存在或过期,返回false     * @param string $id 缓存id     * @return false or data     */    public function load($id){        $this->_cacheid = $id;               $file = $this->getoption('cache_dir') . $this->getoption('cache_prefix') . $this->_cacheid;                       if (@filemtime($file) >= time()){                   return unserialize(file_get_contents($file));         } else {            @unlink($file);            return false;        }    }       /**     * 保存数据缓存,并设置缓存过期时间     * @param array or string $data 要缓存的数据     * @param int $lifetime 缓存过期时间     */    public function save($data, $lifetime = null){           if(null !== $lifetime)            $this->setoption('life_time', $lifetime);           $file = $this->getoption('cache_dir') . $this->getoption('cache_prefix') . $this->_cacheid;        $data = serialize($data);        @file_put_contents($file, $data);        @chmod($file, 0777);        @touch($file, time() + $this->getoption('life_time']));    }          /**     * 读取输出缓存,如果不存在或缓存过期将重新开启输出缓存     * @param string $id 缓存id     */    public function start($id){        $this->_cacheid = $id;        $file = $this->getoption('page_dir') . $this->getoption('cache_prefix') . $this->_cacheid;                       if (@filemtime($file) >= time()){                   return file_get_contents($file);        } else {            @unlink($file);            ob_start();            return false;        }    }    /**     * 删除指定id缓存     * @param string $id 缓存id     */    public function remove($id){        $this->_cacheid = $id;        //删除附合条件的数据缓存        $file = $this->getoption('cache_dir') . $this->getoption('cache_prefix') . $this->_cacheid;        @unlink($file);        //删除附合条件的输出缓存        $file = $this->getoption('page_dir') . $this->getoption('cache_prefix') . $this->_cacheid;        @unlink($file);    }       /**     * 保存输出缓存,并设置缓存过期时间     * @param int $lifetime 缓存过期时间     */    public function end($lifetime = null){        if(null !== $lifetime)            $this->setoption('life_time', $lifetime);           $file = $this->getoption('page_dir') . $this->getoption('cache_prefix') . $this->_cacheid;        $data = ob_get_contents();        ob_end_clean();        @file_put_contents($file, $data);        @chmod($file, 0777);        @touch($file, time() + $this->getoption('life_time']));    }       /**     * 根据参数清除相应缓存     * @param string $mode 缓存类型,包括(cleaning_mode_all:所有缓存, cleaning_mode_old: 过期缓存)     */    public function clear($mode = cleaning_mode_old){           $dirs = array('cache_dir', 'page_dir');        foreach($dirs as $value){            if(null != $this->getoption($value)){                $files = scandir($this->getoption($value));                switch ($mode) {                    case cleaning_mode_all:                    default:                        foreach ($files as $val){                            @unlink($this->getoption($value) . $val);                        }                        break;                    case cleaning_mode_old:                    default:                        foreach ($files as $val){                            if (filemtime($this->getoption($value) . $val) getoption($value) . $val);                             }                        }                        break;                }            }        }    }       /**     * 取临时文件夹为缓存文件夹     * @return $dir 临时文件夹路径     */    public function gettmpdir(){           $tmpdir = array();        foreach (array($_env, $_server) as $tab) {            foreach (array('tmpdir', 'temp', 'tmp', 'windir', 'systemroot') as $key) {                if (isset($tab[$key])) {                    if (($key == 'windir') or ($key == 'systemroot')) {                        $dir = realpath($tab[$key] . '\\temp');                    } else {                        $dir = realpath($tab[$key]);                    }                    if ($this->_isgoodtmpdir($dir)) {                        return $dir;                    }                }            }        }        $upload = ini_get('upload_tmp_dir');        if ($upload) {            $dir = realpath($upload);            if ($this->_isgoodtmpdir($dir)) {                return $dir;            }        }        if (function_exists('sys_get_temp_dir')) {            $dir = sys_get_temp_dir();            if ($this->_isgoodtmpdir($dir)) {                return $dir;            }        }        //通过尝试创建一个临时文件来检测        $tempfile = tempnam(md5(uniqid(rand(), true)), '');        if ($tempfile) {            $dir = realpath(dirname($tempfile));            unlink($tempfile);            if ($this->_isgoodtmpdir($dir)) {                return $dir;            }        }        if ($this->_isgoodtmpdir('/tmp')) {            return '/tmp';        }        if ($this->_isgoodtmpdir('\\temp')) {            return '\\temp';        }        throw new exception('无法确定临时目录,请手动指定cache_dir', e_user_error);    }    /**     * 验证给定的临时目录是可读和可写的     *     * @param string $dir 临时文件夹路径     * @return boolean true or false 临时文件夹路径是否可读写     */    protected function _isgoodtmpdir($dir){           if (is_readable($dir)) {            if (is_writable($dir)) {                return true;            }        }        return false;    }}//endclass
以上就介绍了二级缓存和三级缓存 自己用的php缓存类,包括了二级缓存和三级缓存方面的内容,希望对php教程有兴趣的朋友有所帮助。