|
/*
$cache _set = Array (
Cache path, last add "/"
' CacheRoot ' = './cache/',
Cache time
' CacheTime ' =>20,
Cache type
' CacheType ' =>1,
Extended Name
' Cacheexe ' = '. php '
);
$cache = new Cache ($cache _set);
$a =array (' 1 ', ' 2 ');
$a = "AAA";
$b = ";
if ($cache->cache_is ("D")) {
$c = $cache->cache_read ("D");
echo "C";
Print_r ($c);
}else {
$b = $cache->cache_data (' d ', $a);
}
Print_r ($b);
$cache->clear ("a");
echo $cache->cache_read ("./cache/d.php");
Echo $d;
*/
/**
* Data Cache Class v1.0
* @author Shooke
* 2009-11-13 16:02:26
* Used to cache data, such as variables, but cannot cache pages
*/
Class cache{
Configuration
Public $config = Array (
Cache path
' CacheRoot ' = './cache/',
Cache time
' CacheTime ' =>1,
Cache type 1 Serialization data 2 variable
' CacheType ' =>2,
Extended Name
' Cacheexe ' = '. php '
Converting intermediate variables
);
Public $return _name=array ();
function __construct ($cache _set = Array ())
{
if (!empty ($cache _set)) $this->config=array_merge ($this->config, $cache _set);
$this->config[' ClassName '] = __class__;
}
Public Function Clear ($filename = ") {
if (File_exists ($this->cache_file ($filename))) {
@unlink ($this->cache_file ($filename));
}elseif (Empty ($filename)) {
$this->clear_dir ($this->config[' cacheroot ');
}else{
$this->clear_dir ($this->config[' CacheRoot '). $filename);
echo $this->config[' CacheRoot '). $filename;
}
}
Loop Delete Path
Private Function Clear_dir ($dir, $to = False)
{
if ($list = Glob ($dir. '/* '))
{
foreach ($list as $file)
{
Is_dir ($file)? $this->clear_dir ($file): unlink ($file);
}
}
if ($to = = = False) rmdir ($DIR);
}
Write Cache
Private Function Cache_write ($filename, $writetext, $openmod = ' W ') {
if (!file_exists ($filename)) {
@ $this->makedir (dirname ($filename));
}
if (@ $fp = fopen ($filename, $openmod)) {
Flock ($FP, 2);
Fwrite ($fp, $writetext);
Fclose ($FP);
return true;
} else {
echo "File: $filename write error.";
return false;
}
}
Cache validity Valid Returns True
Public Function cache_is ($fileName) {
$fileName = $this->cache_file ($fileName);
if (file_exists ($fileName)) {
Never expire if the cache time is negative
if ($this->config[' cacheTime ') < 0) {
return true;
}
Expires if the cache time is 0
if ($this->config[' cacheTime ') = = 0) {
return false;
}
Get the cache file settling time
$ctime = Intval (Filemtime ($fileName));
Whether the comparison is greater than the cache time, is expired or is not valid
if (Time ()-$ctime > $this->config[' cacheTime ') {
return false;
}else {
return true;
}
File does not exist as expired expiration
}else {
return false;
}
}
Public Function Cache_data ($name, $data) {
$varname = $name;
$name = $this->cache_file ($name);
config[' CacheTime ']==0 that is, do not enable caching is to return data directly
if ($this->config[' cacheTime ') <> 0) {
if ($this->config[' CacheType ']==1) {
$write _data = " ". Serialize ($data);
return $data;
}else {
$write _data = "
$write _data. = Var_export ($data, true);
$write _data. = ";\\r\\n?>";
}
$this->cache_write ($name, $write _data);
}
return $data;
}
Cache file Name
Private Function Cache_file ($filename) {
return $this->config[' CacheRoot '). $filename. $this->config[' Cacheexe ';
}
Read file
Public Function Cache_read ($file) {
$file = $this->cache_file ($file);
if (!file_exists ($file)) {
Return ';
}
if ($this->config[' CacheType ']==1) {
if (function_exists (' file_get_contents ')) {
$cache _content= file_get_contents ($file);
}else{
$fopen = fopen ($file, ' R ');
$cache _content = ";
do {
$data = Fread ($fopen, FileSize ($file));
if (strlen ($data) ===0) break;
$cache _content. = $data;
}while (1);
Fclose ($fopen);
}
$cache _content = substr ($cache _content,13);/* Remove */
$cache _content = unserialize ($cache _content);
return $cache _content;
}else{
Include_once ($file);
return $var;
}
}
Looping through the creation of catalogs
Private 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;
}
}
?>
|