PHP Dynamic Web site variable static cache speed up the Web site access

Source: Internet
Author: User
Tags define array empty net php file variable access root directory

Some PHP sites are due to upfront design and other reasons
The Web site is dynamic or mostly dynamic, and the database operates very frequently.
Cause Web site access is very slow! Want to implement static, without generating static functionality.
Because I wrote a streamlined process for generating static. It's simple, it's easy to read.
Suitable for basically all systems

Requirements
1. The system supports PHP
2. Support File_get_contents file_put_contents [can also be replaced with functions of the same function]
3. Best support Gzcompress

How to use
1. Download the following shared huncun.php file
2. Create a folder Huancun (or other) in the root directory of the Web site
3. Copy huncun.php to Huancun directory
4. Build folder cache directory under the Huancun directory
5. Load this file on the header of a Web site public file or on a static web page
Include ("/huancun/huancun.php");
6. Empty Cache Reference http://www.oschina.net/code/snippet_202258_7018

Disadvantages
1. The use of the system can not update the content in real time must empty the cache before updating or to the set cache time will be updated
2. The first visit is slower because there is no caching. The second time is to call the cache file, which is fast.
3. cache files for disk space, it is recommended that the server or a larger use of space. Or periodically delete cached files

<?php/*www.diyphp.net *www.php100.com */if ($_get[' Phphuancun ']!= ' true ') {define ("Hc_path", DirName (__file__). " /cache/"); Define ("Hc_time", 1); Echo Hc_getcache (); exit; function Hc_getcache ($iscache = ') {$url = ' http://'. $_server[' server_name '].$_server[' Request_uri ']; $cacheurl = Strpos ($url, "?")? $url. " &phphuancun=true ": $url."? Phphuancun=true "; $cachename =hc_path.md5 ($url). "   C "; $cachetime = $iscache Time () +1:time ()-(Hc_time * 60*60); if (file_exists ($cachename) && filemtime ($cachename) >= $cachetime) {$return =file_get_contents ($cachename $data =function_exists (gzcompress) @gzuncompress ($return): $return; Return Unserialize ($data); }else{$return =file_get_contents ($cacheurl);   Hc_writecache ($cachename, $return); return $return;} function Hc_writecache ($name, $array) {function_exists (gzcompress)? $return =gzcompress (Serialize ($array)): $return =serialize ($array); @file_put_contents ($name, $return); }?>

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.