您的位置:首页 > 编程语言 > PHP开发

【PHP】缓存技术!缓存类,看看原理吧

2016-08-08 15:39 417 查看
<?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');
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: