A comprehensive PHP Cache class parsing code
Warn ('The cached file does not exist and cannot be created. you need to manually create it. '); return false ;}$ this-> cache_dir = $ cache_dirname;} function _ destruct () {echo 'cache class bye. ';} function get_url () {if (! Isset ($ _ SERVER ['request _ URI ']) {$ url = $ _ SERVER ['request _ URI'];} else {$ url = $ _ SERVER ['s cript_NAME ']; $ url. = (! Emptyempty ($ _ SERVER ['query _ string'])? '? '. $ _ SERVER ['query _ string']: '';} return $ url;} function warn ($ errorstring) {echo"Error:".$errorstring."
";} Function cache_page ($ pageurl, $ pagedata) {if (! $ Fso = fopen ($ pageurl, 'w') {$ this-> warns ('cache file cannot be opened. '); // trigger_error return false; if (! Flock ($ fso, LOCK_EX) {// LOCK_NB, arrange it to lock $ this-> warns ('The cache file cannot be locked. '); // trigger_error return false;} if (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats $ this-> warns ('Cannot write to the cache file. '); // trigger_error return false;} flock ($ fso, LOCK_UN); // release the lock fclose ($ fso); return true;} function display_cache ($ cacheFile) {if (! File_exists ($ cacheFile) {$ this-> warn ('The cache file cannot be read. '); // trigger_error return false;} echo 'read cache file :'. $ cacheFile; // return unserialize (file_get_contents ($ cacheFile); $ fso = fopen ($ cacheFile, 'r'); $ data = fread ($ fso, filesize ($ cacheFile); fclose ($ fso); return $ data;} function readdata(your cachefile='default_cache.txt ') {$ cacheFile = $ this-> cache_dir. "/". $ cacheFile; if (file_exists ($ cacheFile) & filemtime ($ cacheFile)> (time ()-$ this-> expireTime )) {$ data = $ this-> display_cache ($ cacheFile);} else {$ data = "from here wo can get it from mysql database, update time is". Date ('l dS \ of f y h: I: s ')."The expiration time is :". date ('l dS \ of f y h: I: s A', time () + $ this-> expireTime ). "----------"; $ this-> cache_page ($ cacheFile, $ data);} return $ data;}/* I will interrupt this code line by line. php learning home http://www.444p.com three, program dialysis this cache class (Class nothing to fear. the name is cache and has two attributes: private $ cache_dir; private $ expireTime = 180; $ cache_dir is the parent directory of the relative Website Directory of the cache file, $ expireTime (note 1) is the time when the cached data expires. The main idea is: when the data or file is loaded, first judge whether the cached file exists and return false, the last modification time and cache time of the file are not greater than the current time. if the file size is large, the cache has not expired. if the file size is small, f is returned. Alse: when false is returned, the system reads the original data, writes it to the cache file, and returns the data ., next look at the program: php learning home http://www.444p.com */function _ construct ($ cache_dirname) {if (! @ Is_dir ($ cache_dirname) {if (! @ Mkdir ($ cache_dirname, 0777) {$ this-> warn ('The cache file does not exist and cannot be created. you need to create it manually. '); return false ;}$ this-> cache_dir = $ cache_dirname;}/* When the class is first instance, construct the default function with the parameter cache file name. if the file does not exist, create a folder with the editing permission and throw an exception when the creation fails. set the $ cache_dir attribute of the cache class to the folder name. all the cached files are under this folder. php learning home */function _ destruct () {echo 'cache class bye. ';} // This is the class destructor. for demonstration, we output a string to indicate that we have successfully released the cache class resources. function warn ($ errorstring) {echo"Error:".$errorstring."
";}/* This method outputs an error message. php */function get_url () {if (! Isset ($ _ SERVER ['request _ URI ']) {$ url = $ _ SERVER ['request _ URI'];} else {$ url = $ _ SERVER ['s cript_NAME ']; $ url. = (! Empty ($ _ SERVER ['query _ string'])? '? '. $ _ SERVER ['query _ string']: '';} return $ url;}/* This method returns information about the current url, this is what I think many foreign cms systems do, mainly cache x. php? Page = 1, x. php? Page = 2, etc. for this type of file, it is listed here for the extension of this cache class function .www.444p.com */function cache_page ($ pageurl, $ pagedata) {if (! $ Fso = fopen ($ pageurl, 'w') {$ this-> warns ('cache file cannot be opened. '); // trigger_errorreturn false;} if (! Flock ($ fso, LOCK_EX) {// LOCK_NB, arrange it to lock $ this-> warns ('The cache file cannot be locked. '); // trigger_errorreturn false;} if (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats $ this-> warns ('Cannot write to the cache file. '); // trigger_errorreturn false;} flock ($ fso, LOCK_UN); // release lock fclose ($ fso); return true ;} /* The cache_page method imports the cached file name and data respectively. this is the method for writing data to the file. open the file with fopen and call the handle to lock the file, then, use fwrite to write the file and release the handle. an error will be thrown in any step. you may see that this comment is written to the byte stream, and serialize is written to other formats. By the way, if we want to write an array (the results can be queried by the select statement in the MySQL database) use the serialize function to write data and unserialize to read the original type. php learning home */function displ Ay_cache ($ cacheFile) {if (! File_exists ($ cacheFile) {$ this-> warn ('The cache file cannot be read. '); // trigger_errorreturn false;} echo 'read cache file :'. $ cacheFile; // return unserialize (file_get_contents ($ cacheFile); $ fso = fopen ($ cacheFile, 'r'); $ data = fread ($ fso, filesize ($ cacheFile); fclose ($ fso); return $ data;}/* This is the way to read the cache by file name. open the file directly and read all, if the file does not exist or cannot be read, false is returned. of course, if you are not human, you can regenerate the cache. */function readdata($cachefile='ulult_cache.txt ') {$ cacheFile = $ this-> cache_dir. "/". $ cacheFile; if (file_exists ($ cacheFile) & filemtime ($ cacheFile)> (time ()-$ this-> expireTime )) {$ data = $ this-> display_cache ($ cacheFile);} else {$ data = "from here wo can get it from mysql database, update time is". Date ('l dS \ of f y h: I: s ')."The expiration time is :". date ('l dS \ of f y h: I: s A', time () + $ this-> expireTime ). "----------"; $ this-> cache_page ($ cacheFile, $ data);} return $ data;}/* this function is the method we call and can be written as an interface method, the input parameter determines whether the file exists or not. if the last modification time of the file + expireTime expires, the file does not expire. if the file does not exist or has expired, reload the original data. here, for the sake of simplicity, we directly source the data as a string. you can inherit a class of cache class and obtain the data of the database. (Note 2) php learning home IV. supplementary description. Conclusion 1: You can adjust the cache time by yourself. you can read arrays, xml files, and cache files based on time conditions, please follow your convenience. it is worth mentioning that the cache time (that is, the cache key) also uses cache control ,. this is widely used in the cms system. In the cache, it is very easy to control the entire battle. php learning home Note 2: php5 started to support class inheritance, which is exciting. it writes the Global rest of the website into a configured class, then write the classes that interact with the data layer (such as those that interact with MySQL). Our cache class inherits the classes that interact with data and can easily read the database, I will not discuss it here. I have time to discuss it with you. note that this class file is applicable to php5 and later versions. do not use classes for other versions. */?>