Detailed descriptions of PHP Cache operation instances. Why the cache technology? The reason is simple: improve efficiency. In program development, the primary way to obtain information is to query the database. In addition, it may also be through WebServices or why the cache technology is used? The reason is simple: improve efficiency. In program development, the primary way to obtain information is to query the database. In addition, it may also be through Web Services or some other method. no matter which method, in the face of a large number of concurrent accesses, they may all become efficiency bottlenecks. to solve these problems, many solutions are proposed, some of which use optimization software (such as APC, Eaccelerator, Zend Optimizer, and so on) to improve the running efficiency of the program, the rational use of these software can often improve the running efficiency of the program by an order of magnitude, but the premise is that you must control the host, so that you can install the software. if you are using a virtual host, you can only pray that your service provider has pre-installed an optimization software, otherwise, you must use PHP to implement the corresponding cache function. If this makes you feel at a loss, I believe the following cache operation code can give you some useful inspiration. (PHP Cache operation instance details)
CachePath = emptyempty ($ Path )? $ This-> cachePath: $ Path; $ this-> cachetime = emptyempty ($ time )? $ This-> cachetime: $ time; $ file = $ this-> cachePath. $ id. '. php '; if (file_exists ($ file) {// cache expiration if (filemtime ($ file) + $ time)
ReturnType) {$ row = include $ file;} else {$ data = file_get_contents ($ file); $ row = unserialize ($ data);} return $ row ;} return NULL;}/*** write cache ** @ accesspublic * @ param mixed $ data cache content * @ returnbool whether the write is successful */public function writeCache ($ id, $ data, $ Path = '') {$ this-> cacheCount ++; $ id = md5 ($ id); $ this-> cachePath = emptyempty ($ Path )? $ This-> cachePath: $ Path; $ file = $ this-> cachePath. $ id. '. php'; chmod ($ this-> cachePath, 777); if (1 ===$ this-> returnType) {$ data ='
';} Else {$ data = serialize ($ data);} return file_put_contents ($ file, $ data );} /*** delete the specified cache ** @ accesspublic * @ param mixed $ id: indicates whether the cache name * @ returnbool is successfully deleted */public function delCache ($ id, $ Path = '') {$ id = md5 ($ id); $ this-> cachePath = emptyempty ($ Path )? $ This-> cachePath: $ Path; $ file = $ this-> cachePath. $ id. '. php '; if (file_exists ($ file) {return unlink ($ file);} return NULL ;} /*** delete all caches ** @ accesspublic * @ param mixed $ dir cache name * @ returnbool whether all caches are cleared successfully */public function delAllCache ($ Path = '') {$ id = md5 ($ id); $ this-> cachePath = emptyempty ($ Path )? $ This-> cachePath: $ Path; $ path = $ this-> cachePath; if (is_dir ($ path) {if ($ dh = opendir ($ path )) {while ($ file = readdir ($ dh ))! = False) {if ($ file! = '..' & $ File! = '.') {If (is_dir ($ path. '/'. $ file) {if (! DelDir ($ path. '/'. $ file) {return 0 ;}} else {if (! Unlink ($ path. '/'. $ file) {return 0 ;}}} closedir ($ dh) ;}return 1 ;}}}
The reference method of the cache operation class is as follows:
WriteCache ($ id, $ data); // read the cache and print $ name_row = $ cache-> readCache ($ id, 120); print_r ($ name_row ); // delete a variable $ cache-> delCache ($ id); // delete all caches $ cache-> delAllCache ();
Make sure that the cache Directory (that is, the cache directory) exists and can be written.
Articles you may be interested in
- Php obtains the current operating system type
- Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
- PHP generates instances of download links for thunder, express train, and QQ tornado.
- How does php determine whether the current operating system is linux or windows?
- Php uses the ZipArchive function to compress and decompress files.
- Php implements multiple inheritance instances
- PHP Cache technology details
- Php obtains all the files in the directory and saves the results to the array program.
Why? The reason is simple: improve efficiency. In program development, the primary way to obtain information is to query the database. In addition, it may also be through Web Services or...