Why use caching technology? The reason is simple: improve efficiency. In program development, the main way to obtain information is to query the database. In addition, it may also be through Web Services or some other method. No matter which method is used, they may become an efficiency bottleneck in the face of a large number of concurrent accesses. , in order to solve these problems, people have proposed many solutions, some of which use optimization software (such as: APC, Eaccelerator, Zend Optimizer, etc.) to improve the running efficiency of the program. Reasonable use of these software can often make the program run smoothly. The efficiency has been improved by orders of magnitude, but the premise is that you must have control of the host to be able to install these software. If you are using a virtual host, you can only pray that your service provider has pre-installed some optimization software. , otherwise you must use PHP yourself to implement the corresponding caching function. If this makes you feel at a loss, I believe the following cache operation code can give you some useful inspiration. (Detailed explanation of PHP cache operation examples)
<?php /** +---------------------------------------------------------- * franklin 缓存操作类 +---------------------------------------------------------- * 文件名称 cache.php +---------------------------------------------------------- * 文件描述 缓存操作类 +---------------------------------------------------------- * 作 者 http://www.phpernote.com +---------------------------------------------------------- * 时 间 2012-05-05 +---------------------------------------------------------- */ class cache{ //查询参数 protected $options=array(); //缓存次数 protected $cacheCount=0; //缓存时间 protected $cachetime=60; //缓存路径 protected $cachePath='cache/'; //数据返回类型, 1代表数组, 2代表对象 protected $returnType=1; /** * 读取缓存 * @param string $id 缓存名称 * @param int $time 缓存有效时间,默认为60秒,0为永远失效 * @param string $path缓存文件存放路径 * @accesspublic readCache * @returnmixed如果读取成功返回缓存内容, 否则返回NULL */ public function readCache($id,$time,$Path=''){ $id=md5($id); $this->cachePath=emptyempty($Path)?$this->cachePath:$Path; $this->cachetime=emptyempty($time)?$this->cachetime:$time; $file=$this->cachePath.$id.'.php'; if(file_exists($file)){ //缓存过期 if((filemtime($file)+$time)<time()){ @unlink($file); return NULL; } if(1===$this->returnType){ $row=include $file; }else{ $data=file_get_contents($file); $row=unserialize($data); } return $row; } return NULL; } /** * 写入缓存 * * @accesspublic * @param mixed$data缓存内容 * @returnbool是否写入成功 */ public function writeCache($id,$data,$Path=''){ $this->cacheCount++; $id=md5($id); $this->cachePath=emptyempty($Path)?$this->cachePath:$Path; $file=$this->cachePath.$id.'.php'; chmod($this->cachePath,777); if(1===$this->returnType){ $data='<?php return '.var_export($data, TRUE).'; ?>'; }else{ $data=serialize($data); } return file_put_contents($file, $data); } /** * 删除指定缓存 * * @accesspublic * @param mixed$id缓存名称 * @returnbool是否删除成功 */ public function delCache($id,$Path=''){ $id=md5($id); $this->cachePath=emptyempty($Path)?$this->cachePath:$Path; $file=$this->cachePath.$id.'.php'; if(file_exists($file)){ return unlink($file); } return NULL; } /** * 删除所有缓存 * * @accesspublic * @param mixed$dir缓存名称 * @returnbool清除所有缓存是否成功 */ public function delAllCache($Path=''){ $id=md5($id); $this->cachePath=emptyempty($Path)?$this->cachePath:$Path; $path=$this->cachePath; if(is_dir($path)){ if($dh=opendir($path)){ while(($file=readdir($dh))!==false){ if($file!='..'&$file!='.'){ if(is_dir($path.'/'.$file)){ if(!delDir($path.'/'.$file)){ return 0; } }else{ if(!unlink($path.'/'.$file)){ return 0; } } } } closedir($dh); } return 1; } } }
The reference method of the above cache operation class is as follows:
<?php include('cache.php'); $data=array('http://www.phpernote.com','http://www.baidu.com','http://www.google.cn'); $cache=new cache(); $id='test'; //写入缓存 $cache->writeCache($id,$data); //读缓存并打印 $name_row=$cache->readCache($id,120); print_r($name_row); //删除某个变量 $cache->delCache($id); //删除全部缓存 $cache->delAllCache();
Be sure to ensure that the cache directory (i.e. cache directory) exists and is writable.