【PHP】緩存技術!緩存類,看看原理吧

<?php

	

	/**
	* 	緩存
	*/
	class Cache
	{

		private $cachedir	=	'./cache/'; //目錄
		private $cachetime  =	0;			//更新時間
		private $cachename	=	"";			//文件名稱
		private $cachefile	=	".php";		//文件後綴名
		


		function __construct($cachetime)
		{
			if(intval($cachetime)){
				$this->cachetime =$cachetime;
				$this->cachename=$this->getFileName();
				ob_start();
			}
		}

		/**
		 * [cacheCheck 檢測緩存是否過時]
		 * @return [type] [description]
		 */
		public function cacheCheck($name=''){
			$name=$this->pathSet($name);
			if(file_exists($name))
			{
				$ctime=$this->createCacheTime($name);
				if ($ctime+$this->cachetime>time()) {
					echo "【如下爲緩存】";
					echo file_get_contents($name);
					ob_end_flush();

					exit();
				}
			}
			elseif(file_exists($this->cachename))
			{
				$ctime=$this->createCacheTime($this->cachename);
				if ($ctime+$this->cachetime>time()) {
					echo "【如下爲緩存】";
					echo file_get_contents($this->cachename);
					ob_end_flush();

					exit();
				}
			}
			return false;
		}
		/**
		 * [pathSet 名稱格式化]
		 * @param  [type] $name [description]
		 * @return [type]       [description]
		 */
		public function pathSet($name){
			return "./".$name;
		}
		/**
		 * [putCache 寫出緩存]
		 * @param  string $name [description]
		 * @return [type]       [description]
		 */
		public function putCache($name=''){
			if ($this->cachename) {

				$content =	ob_get_contents(); //獲取當前頁面的全部內容

				ob_end_flush();

				if ($name) {
					$this->saveCache($name,$content);
				}

				elseif($this->cachetime){
					$this->saveCache($this->cachename,$content);
				}
			}
		}
		/**
		 * [saveCache 保存緩存]
		 * @param  [type] $name    [description]
		 * @param  [type] $content [description]
		 * @return [type]          [description]
		 */
		public function saveCache($name,$content){
			if (!$name || !$content) return false;

			if ($this->makeDir(dirname($name))) {
				if ($fp=fopen($name,"w")) {
					if (@fwrite($fp,$content)) {
						fclose($fp);
						return true;
					}else{
						fclose($fp);
						return false;
					}
				}
			}

			return false;
		}
		/**
		 * [clearCache 清除緩存]
		 * @param  string $fileName [description]
		 * @return [type]           [description]
		 */
		public function clearCache($fileName = "all") {  
			if($fileName !="all"){
				$fileName = $this->cachedir.strtoupper(md5($fileName)).$this->cachefile;

				if (file_exists($fileName)) {
					return @unlink($fileName);
				}else{
					return false;
				}
			}

			if(is_dir($this->cachedir)){
				if ($dir = @opendir($this->cachedir)) {
					while ($file = @readdir($dir)) {
						$check = is_dir($file);
						if (!$check) {
							@unlink($this->cachedir.$file);
						}
					}

					@closedir($dir);
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
		} 
		/**
		 * [makeDir 建立文件]
		 * @param  [type] $dirname [description]
		 * @return [type]          [description]
		 */
		public 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;
		}

		/**
		 * [createCacheTime 建立緩存時間名]
		 * @param  [type] $cachename [description]
		 * @return [type]            [description]
		 */
		public function createCacheTime($cachename){
			if(!trim($cachename))return 0;
			if(file_exists($cachename)){
				return intval(filemtime($cachename));
			}else{
				return 0;
			}
		}
	   /**
		* [getFileName 生成一個緩存文件名]
		* @return [type] [description]
		*/
		public function getFileName(){
			return $this->cachedir.strtoupper(md5($_SERVER['REQUEST_URI'])).$this->cachefile;
		}

	}

index.phpphp

<?php

	include $_SERVER['DOCUMENT_ROOT']."/slike/cache.php";

   	$cache=new Cache(30);
   	//$cache->clearCache();

	if(!$cache->cacheCheck('111.php')){
	   	echo file_get_contents($_SERVER['DOCUMENT_ROOT']."/slike/view/index.html");  
		echo date("Y-m-d H:i:s");
		$cache->putCache('111.php'); 
	}
相關文章
相關標籤/搜索