Php implements tool-class sharing for web cache, and php web cache tool-class _ PHP Tutorial

Source: Internet
Author: User
Php implements tool class sharing for web page caching, and php web page caching tool class. Php implements tool-type sharing for web cache. php tool-type php programs are often difficult to attract dynamic websites when they resist high-traffic access. Therefore, the cache mechanism should be introduced, in general, php implements tool-class sharing for web page caching, and php web page caching tool-class

When php programs defend against high-traffic access, dynamic websites are often difficult to attract. Therefore, the cache mechanism should be introduced. Generally, there are two types of cache.

I. file cache

II. cache data query results and use memory for high-speed caching

In this example, file caching is used.

The main principle is to use the cache function to store the webpage display results. if you call it again within the specified time, you can load the cached file.

Tool code:

// File Cache class Cache {/*** $ dir: Cache file storage directory * $ lifetime: Cache file validity period, in seconds * $ cacheid: Cache file path, include file name * $ ext: cache file extension (optional), which is used here for file viewing convenience */private $ dir; private $ lifetime; private $ cacheid; private $ ext; /*** destructor: Check whether the cache directory is valid. the default value is */function _ construct ($ dir = '', $ lifetime = 1800) {if ($ this-> dir_isvalid ($ dir) {$ this-> dir = $ dir; $ this-> lifetime = $ lifetime; $ this-> ext = '. php'; $ this-> cachei D = $ this-> getcacheid () ;}}/*** check whether the cache is valid */private function isvalid () {if (! File_exists ($ this-> cacheid) return false; if (! (@ $ Mtime = filemtime ($ this-> cacheid) return false; if (mktime ()-$ mtime> $ this-> lifetime) return false; return true ;} /*** write cache ** $ mode = 0. obtain the page content in browser cache * $ mode = 1 and assign values directly (received by the $ content parameter) obtain the page content in the form of * $ mode = 2, and obtain the page content locally (fopen ile_get_contents) (it seems that this method is unnecessary) */public function write ($ mode = 0, $ content = '') {switch ($ mode) {case 0: $ content = ob_get_contents (); break; d Efault: break;} ob_end_flush (); try {file_put_contents ($ this-> cacheid, $ content);} catch (Exception $ e) {$ this-> error ('write cache failed! Check the directory permission! ') ;}}/*** Load cache * exit () after loading the cache, terminate the execution of the original page program. if the cache is invalid, run the original page program to generate the cache * ob_start () enable browser cache to get the page content at the end of the page */public function load () {if ($ this-> isvalid () {// either of the following methods, which method is better ????? Require_once ($ this-> cacheid); echo"
 "; // Echo file_get_contents ($ this-> cacheid); exit () ;}else {ob_start () ;}/ *** clear cache */public function clean () {try {unlink ($ this-> cacheid);} catch (Exception $ e) {$ this-> error ('failed to clear the cache file! Check the directory permission! ') ;}}/*** Obtain the cache file path */private function getcacheid () {return $ this-> dir. md5 ($ this-> geturl ()). $ this-> ext;}/*** check whether the directory exists or can be created */private function dir_isvalid ($ dir) {if (is_dir ($ dir) return true; try {mkdir ($ dir, 0777);} catch (Exception $ e) {$ this-> error ('The specified cache directory does not exist and creation failed! Check the directory permission! '); Return false;} return true;}/*** get the complete url of the current page */private function geturl () {$ url = ''; if (isset ($ _ SERVER ['request _ URI ']) {$ url = $ _ SERVER ['request _ URI'];} else {$ url = $ _ SERVER ['php _ SELF ']; $ url. = empty ($ _ SERVER ['query _ string'])? '':'? '. $ _ SERVER ['query _ string'];} return $ url;}/*** output error message */private function error ($ str) {echo'

'. $ Str .'

';}}

Usage:

The usage is as follows:

Some code is placed before the logic code to be cached:

$ Cachedir = '. /Cache/'; // Set the cache directory $ Cache = new Cache ($ cachedir, 33); // if the parameter is omitted, the default setting is used, $ cache = new Cache ($ cachedir); if (@ $ _ GET ['cacheac']! = 'Rewrite' | @ $ _ GET ['Your ache'] = 'OK') // This is a tip, Via xx. Php? Cacheact = rewrite update cache, and so on. you can also set some other operations $ cache-> load (); // load the cache. if the cache is valid, the following page code is not executed. // The page code starts.

Some are placed behind the cached logic code:

// The page code ends $ cache-> write (); // The first time the code is run or the cache expires, the cache is generated

The above is all the content of this article. I hope you will like it.

Dynamic php programs are often difficult to attract dynamic websites when they resist high-traffic access. Therefore, the cache mechanism should be introduced...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.