Cache classification in PHP database cache, file cache and memory Cache , I'll give you a detailed introduction to the PHP file cache class implementation code, there is a need to know friends can refer to. The
page Cache class
code is as follows &NBSP:
<?php/*include ("cache.php");
$cache = new cache (30);
$cache->cachecheck ();
echo Date ("y-m-d h:i:s"); $cache->caching ();
* * Class Cache {//cache directory var $cacheRoot = "./cache/";
Cache update time seconds, 0 is not cached var $cacheLimitTime = 3;
Cached filename var $cacheFileName = "";
Cache name extension var $cacheFileExt = "PHP"; /* constructor * int $cacheLimitTime cache update time/function cache ($cacheLimitTime) {if (intval) ($cacheLimitTi
Me) $this->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, the return failure/function Cachecheck () {if file_exists ($this->cachefilename))
{$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 ();
Echo $cacheContent;
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 succeeds return true, Vice return false */function ClearCache ($fileName = "All") {if ($fileName!= ' all ') {$fileName = $this->cacheroot. Strtoupper (MD5 ($FI lename)). ".".
$this->cachefileext;
if (file_exists ($fileName)) {return @unlink ($fileName);
}else return false; } if (Is_dir ($this->cacheroot)) {if ($dir = @opendir ($this->cacheroot)) {while ($file = @r Eaddir ($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 creation time * string $fileName cache file name (including relative path) * Return: File generation time seconds, file does not exist return 0/function getfilecreat
ETime ($fileName) {if (! Trim ($fileName)) 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/F
Unction SaveFile ($fileName, $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 has been built returns TRUE, otherwise returns false */Func
tion 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;
}}?>
The above use is a page cache, each visit to the page, will first detect the corresponding cache page file exists, if it does not exist, connect the database, get the data, display the page and generate cached page file, so the next time the page file will play a role. (the template engine and some common online caching classes typically have this feature)
To introduce a memcache cache , is a memory cache.
The code is as follows
<?php
$memcache = new Memcache;
$memcache->connect (' localhost ', 11211) or die ("could not Connect");
$version = $memcache->getversion ();
echo "Server" version: ". $version." n ";
$tmp _object = new StdClass;
$tmp _object->str_attr = ' Test ';
$tmp _object->int_attr = 123;
$memcache->set (' key ', $tmp _object, False, All) or Die ("Failed to save data at the server");
echo "Store data in the cache" (data would expire in seconds) n ";
$get _result = $memcache->get (' key ');
echo "Data from the Cache:n";
Var_dump ($get _result);
? >
Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications.
The above is the entire content of this article, I hope to learn PHP caching help.