Cache classification database cache, file cache and memory cache in PHP. Now I will give you a detailed introduction to the PHP file cache class implementation code. Friends who need to know more can refer to it.
Page cache class
The code is as follows | Copy code |
/* * Cache class cache * Author: Duo noobiao * Example: */ /*include( "cache.php" ); $cache = new cache(30); $cache->cacheCheck(); echo date("Y-m-d H:i:s"); ;caching(); */
class cache { //Cache directory var $cacheRoot = "./cache/"; //Cache update time in seconds, 0 means no caching var $cacheLimitTime = 3 ; //Cache 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 cache file is being updated Within the time * Return: If it is within the update time, return the file content, otherwise 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 ); > /* * Cache files 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 ); 🎜> } } /* * Clear cache files * string $fileName specifies the file name (including function) or all (all) * Return: Return true if clearing is successful, Otherwise return 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 ) ) { read while ( $file = @dir ( $dir ) ) { $check = is_dir( $file ); if ( !$check ) @unlink( $this->cacheRoot . $file ); } } @closedir( $dir ); return true; 🎜> } /* * Generate a cache file name based on the 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 : The file generation time in seconds. If the file does not exist, 0 will be returned. */ function getFileCreateTime( $fileName ) { if( ! trim($fileName) ) return 0; file_exists( $fileName ) ) { return intval(filemtime( $fileName )); }else return 0; } /* * Save the file * string $fileName File name (including relative path) * string $text File content * Return: true on success, false on failure */ 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 { ($fp); ; * * Continuously create directories * string $dir directory string * int $mode permission number * Return: true if created successfully or all have been created, false otherwise */ 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(!@mkdir( $mdir, $mode )){ } ? >
The above usage is regarded as page caching. Every time a page is accessed, it will first detect whether the corresponding cached page file exists. If it does not exist, it will connect to the database. Get the data, display the page and generate a cache page file at the same time, so that the page file will be effective the next time you visit. (Template engines and some common cache classes on the Internet usually have this function)
Later I will introduce you to a Memcache cache, which is considered a memory cache
| Code
Copy code |
< ?php $memcache = new Memcache;$memcache->connect('localhost', 11211) or die ("Could not connect"); $version = $memcache->getVersion( ); 代码如下 | 复制代码 | $memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server's version: ".$version."n"; $tmp_object = new stdClass; $tmp_object->str_attr = 'test'; $tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server"); echo "Store data in the cache (data will expire in 10 seconds)n"; $get_result = $memcache->get('key'); echo "Data from the cache:n"; var_dump($get_result); ?> | echo "Server's version: ".$version."n";$tmp_object = new stdClass; $tmp_object->str_attr = 'test'; | $tmp_object-> int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache ( data will expire in 10 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 used to reduce database load and improve access speed in dynamic applications.
http://www.bkjia.com/PHPjc/444585.htmltruehttp: //www.bkjia.com/PHPjc/444585.htmlTechArticleCache classification database cache, file cache and memory cache in php. Let me introduce PHP files to you in detail. Cache class implementation code, friends who need to know more can refer to it. Page...