Page Caching Classes
The code is as follows |
Copy Code |
<?php /* * Cache class Caching * Author: More than a rookie Instance */ /*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 file name var $cacheFileName = ""; Cache extension Name var $cacheFileExt = "PHP";
/* * Constructor * int $cacheLimitTime Cache update Time */ function Cache ($cacheLimitTime) { if (Intval ($cacheLimitTime)) $this->cachelimittime = $cacheLimitTime; $this->cachefilename = $this->getcachefilename (); Ob_start (); }
/* * Check whether the cached files are in the set update time * Return: If the content of the file is 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 returns false */ function ClearCache ($fileName = "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 creation time * String $fileName cache file name (including relative path) * Return: File generation time of seconds, file does not exist return 0 */ function Getfilecreatetime ($fileName) { if (! Trim ($fileName)) return 0;
if (file_exists ($fileName)) { Return Intval (Filemtime ($fileName)); }else return 0; }
/* * Save File * String $fileName file name (including relative path) * String $text file contents * Return: Successfully returned ture, failed return false */ function 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 constructed returns True, otherwise returns 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; } } ?> |
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)
Later to introduce a memcache cache, is a memory cache
Code
The code is as follows |
Copy Code |
<?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.