<?php
/* 页面缓存机制
* 摘要:页面缓存机制
* 类名:fanCache
* 文件名称:fanCache.inc.php
* 版本:1.0.0
* 创建时间:2007.2.20 22:00
* 作者:李云帆(asuperboylyf@163.com)
* 网站:www.cq100.cn
* 版权:COPYRIGHT(C) 2006-2007 李云帆
* 转载请保留头信息
* 具体功能:
可以缓存一般类型、数组。具有按需调用,有效期设置,模块形式保存。
可能有一些BUG,可以向我发E-MAIL。
*/
/* 使用方法
请先设置全局变量
require_once("fanCache.inc.php");
$cache = new fanCache();
if(!$cache -> isMode("c1"))
$cache -> create("c1");
if(!$cache->isLoad("c1"))
$cache -> load("c1");
if(!$cache->isCached("c1","p1"))
{
$v = array("sdfsdfsdf","sdfsdfsdfsdfsdfds"=>1);
$cache -> set("c1","p1",$v);
}
echo $cache -> get("c1","p1");
*/
define("CACHE_ROOT",$_SERVER['DOCUMENT_ROOT']."/cache/");//缓存目录,相对路径,带/
define("CACHE_INDEX","main.php");
define("DEFAULT_TIME",3600);//默认有效期,单位,秒
class fanCache
{
var $edit=false;
var $index=0;
var $data=0;
function fanCache()
{
$this -> edit = false;
if(!file_exists(CACHE_ROOT.CACHE_INDEX))
{
$this -> index = array();
$this -> edit = true;
if( ! $this -> saveIndex())
throw new Exception("Cache file isn't exists and it cannot create cache index file!");
}
require_once(CACHE_ROOT.CACHE_INDEX);
$this -> index = $GLOBALS['_cache_index'];
$this -> data = array();
unset($GLOBALS['_cache_index']);
}
private function saveIndex()
{
if( ! $this -> edit)
return true;
$content = arrayToString($this -> index);
$content = '<? $GLOBALS["_cache_index"]='.$content.';?>';
if(!is_dir(CACHE_ROOT))
@ mkdir(CACHE_ROOT,0777);
return @ file_put_contents(CACHE_ROOT.CACHE_INDEX,$content);
}
function end()
{
$this -> saveAll();
$this -> saveIndex();
}
function __destruct()
{
$this -> end();
}
function set($mode,$key,$value)
{
if( ! $this -> isLoad($mode))
return false;
if( ! $this -> isCached( $mode , $key ))
{
$this -> index[ $mode ][ "item" ][ $key ] = true;
$this -> index[ $mode ][ "edit" ] = true;
$this -> edit = true;
}
return $this -> data[ $mode ][ $key ] = $value;
}
function get($mode, $key)
{
if( ! $this -> isLoad($mode))
return false;
if( ! $this -> isCached( $mode, $key))
return false;
return $this -> data[ $mode ][ $key ];
}
function isCached($mode, $key)
{
if( ! $this -> isMode( $mode ))
return false;
return isset($this -> index[ $mode ][ "item" ][ $key ]);
}
function isExists($mode)
{
if(!$this->isMode($mode))
return false;
return $this->index[$mode]['exists']>=time();
}
function isMode($name)
{
if(!file_exists(CACHE_ROOT.'c_'.$name.'.php'))
$this -> delete($name);
return isset($this -> index[$name]);
}
function isLoad($name)
{
return $this -> index[$name]["load"];
}
private function saveAll()
{
foreach( $this -> index as $key => $value)
{
if( $value["edit"])
{
$this -> close( $key );
}
$this -> index[ $key ]["load"] = false;
}
}
function load($name)
{
if(!$this->isMode($name))
return false;
if($this -> isLoad( $name ))
return true;
@ require(CACHE_ROOT.'c_'.$name.'.php');
$this -> data[ $name ] = $GLOBALS['_c_'.$name];
unset($GLOBALS['_c_'.$name]);
$this -> index [ $name ]["load"] = true;
return true;
}
private function save($name)
{
if(!$this->isLoad($name))
return false;
if(!$this->index[$name]["edit"])
return false;
$content = '<? $GLOBALS[/'_c_'.$name.'/']=';
$content .=arrayToString($this -> data [$name]);
$content .=';?>';
$this -> index[ $name ][ "edit" ] = false;
if(! @ file_put_contents(CACHE_ROOT.'c_'.$name.'.php',$content))
return false;
return true;
}
function delete($name)
{
unset( $this -> data[ $name ]);
unset( $this -> index[ $name ]);
$this -> edit = true;
@ unlink(CACHE_ROOT.'c_'.$name.'.php');
}
function out( $mode, $key)
{
if(!isLoad($mode,$key))
return false;
unset($this -> data[ $mode][ $key]);
unset($this -> index[ $mode]["item"][ $key]);
$this -> index[ $mode]["edit"] = true;
$this -> edit = true;
return true;
}
function close($name)
{
$this -> save( $name );
unset( $this -> data[ $name ]);
$this -> index[ $name ]["load"] = false;
}
function create($name,$time=DEFAULT_TIME)
{
$time += time();
$this -> data [$name] = array();
$this -> index[ $name ][ "edit" ] = true;
$this -> index[ $name ][ "load" ] = true;
$this -> index[ $name ][ "item" ] = array();
$this -> index[$name]["exists"]=$time;
$this -> edit = true;
return $this -> save( $name );
}
function update($name,$time=DEFAULT_TIME)
{
if(!isset($this->index[$name]))
return false;
$this->index[$name]["exists"] =$time+time();
$this->edit=true;
}
}
//函数 arrayToString 参数:数组 返回:字符 功能:把数组转换为PHP代码
function arrayToString($tochange)
{
$content='array(';
$comma="";
foreach($tochange as $key => $value)
{
if( ! (is_int( $key )||is_long( $key )))
$key = "'".$key."'";
$content .= $comma.$key.'=>';
if(is_array($value))
$content .= arrayToString($value);
else
{
if(is_int($value)||is_long($value))
$content .= $value;
else
$content .= "'".$value."'";
}
$comma=',';
}
$content.=')';
return $content;
}
?>