A good php file page cache class. Cache Classification Database cache, file cache, and memory cache in php. I will introduce the php file cache implementation code in detail to you. if you have any questions, please refer. The pages are cached in php for Classification Database cache, file cache, and memory cache. next I will introduce you to php file cache implementation code in detail. if you need to know, refer.
Page cache
The code is as follows: |
|
/* * Cache * Author: many Cainiao * Example: */ /* 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 in seconds. 0 indicates no cache. Var $ cacheLimitTime = 3; // Cache file name Var $ cacheFileName = ""; // Cache extension 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 updated within the specified update time. * Return: If the file content is returned within the update time, otherwise, an error is returned. */ 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 cached files * String $ fileName specifies the file name (including the function) or all (all) * Return: true is returned if the job is cleared successfully, and false is returned if the job is cleared successfully. */ Function compute ache ($ 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 names 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: file generation time in seconds. if the file does not exist, 0 is returned. */ Function getFileCreateTime ($ fileName ){ If (! Trim ($ fileName) return 0; If (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 is returned successfully. false is returned if a failure occurs. */ 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 directory creation * String $ dir directory string * Int $ mode permission number * Return value: "true" is returned for successful creation or full creation. otherwise, "false" is returned. */ 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 is regarded as page cache. every time you access the page, the system first checks whether the cached page file exists. if it does not exist, it connects to the database to obtain data, the page is displayed and cached page files are generated at the same time, so that the page files will play a role in the next visit. (The template engine and some common cache classes on the Internet usually have this function)
Later, I will introduce you to a Memcache cache, which is a memory cache.
Code
The code is as follows: |
|
$ Memcache = new Memcache; $ Memcache-> connect ('localhost', 11211) or die ("cocould 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 ); ?> |
Memcached is a high-performance, distributed memory object cache system that reduces database loads in Dynamic Applications and improves access speeds.
Bytes. Page...