【PHP】缓存技术!缓存类,看看原理吧
博客专区 > RablePHP 的博客 > 博客详情
【PHP】缓存技术!缓存类,看看原理吧
RablePHP 发表于1年前
【PHP】缓存技术!缓存类,看看原理吧
  • 发表于 1年前
  • 阅读 34
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: 【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.php

<?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'); 
	}


 






共有 人打赏支持
粉丝 14
博文 48
码字总数 12000
×
RablePHP
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: