This article describes a concise and practical PHP caching class, which can be used to check whether the cached files are set to update time, clear cache files, generate cached file names based on current dynamic files, create directories continuously, and cache file output static functions. To use PHP to develop CMS system, it is inseparable from the processing of the cache, reasonable use of good caching can effectively improve program execution efficiency.
PHP Cache class file complete code is as follows:
<?php * * Cache class Cached/class Cache {//cache directory var $cacheRoot = "./cache/";//cache update time seconds, 0 is not cached var $cacheLimitTime = 0;
Cached filename var $cacheFileName = "";
Cache name extension var $cacheFileExt = "PHP"; /* constructor * int $cacheLimitTime cache update time/function cache ($cacheLimitTime) {if (Intval ($cacheLimitTime)) $t
His->cachelimittime = $cacheLimitTime;
$this->cachefilename = $this->getcachefilename ();
Ob_start (); * * * Check whether the cached file is within the SET update time * Returns: if the contents of the file are returned within the update time, then the failure/function Cachecheck () {if file_exists ($this->cache
FileName)) {$cTime = $this->getfilecreatetime ($this->cachefilename);
if ($cTime + $this->cachelimittime > Time ()) {echo file_get_contents ($this->cachefilename);
Ob_end_flush ();
Exit
return false; /* * cache file or output static * string $staticFileName static file name (including relative path)/function caching ($staticFileName = "") {if ($this
Cachefilename) {$cacheContent = Ob_get_contents ();
Ob_end_flush (); if ($staticFileName) {$this->savefile ($staticFileName, $cacheContent);
if ($this->cachelimittime) $this->savefile ($this->cachefilename, $cacheContent); }/* * Clear Cache file * String $fileName Specify file name (including function) or all (full) * Return: Purge successfully returns true, Vice return false/function ClearCache ($fileNam E = "All"} {if ($fileName!= "all") {$fileName = $this->cacheroot. Strtoupper (MD5 ($fileName)). "."
$this->cachefileext;
if (file_exists ($fileName)) {return @unlink ($fileName);
}else return false; } if (Is_dir ($this->cacheroot)) {if ($dir = @opendir ($this->cacheroot)) {while ($file = @readdir ($
dir)) {$check = Is_dir ($file);
if (! $check) @unlink ($this->cacheroot. $file);
} @closedir ($dir);
return true;
}else{return false;
}}else{return false; */* Generate cache file name based on current dynamic file/function Getcachefilename () {return $this->cacheroot. Strtoupper (MD5 $_server[Request_uri "])).".".
$this->cachefileext; * * * Cache file build time
* String $fileName cache file name (including relative path) * Returns: File generation time seconds, file does not exist returns 0/function Getfilecreatetime ($fileName) {if (! Trim ($f
ilename)) return 0;
if (file_exists ($fileName)) {return intval (Filemtime ($fileName));
}else return 0; * * * Save file * string $fileName filename (including relative path) * String $text file content * Return: Successfully returned ture, failed return false/function SaveFile ($fil
ename, $text) {if (! $fileName | |! $text) return false;
if ($this->makedir (dirname ($fileName)) {if ($fp = fopen ($fileName, "w")) {if (@fwrite ($FP, $text)) {
Fclose ($FP);
return true;
}else {fclose ($FP);
return false;
}} return false; * * * Continuous build directory * String $dir directory String * int $mode Permission number * return: Successfully created or all built returns True, otherwise return false/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; }}?>
When using this caching class, you can save the above code as cache.php, as shown in the following example:
Include ("cache.php");
$cache = new cache (m);
$cache->cachecheck ();
echo Date ("y-m-d h:i:s");
$cache->caching ();