Php page cache ob series functions

Source: Internet
Author: User
I have been touched by the page cache of phpcms over the past few days. The advantage is not much said. It is generally used on pages with a large number of database queries. it is not suitable for inserting, modifying, and deleting pages.

Here is a brief introduction to the cache technology: http://www.bitsCN.com/article/4965.htm

Php page cache mainly uses ob functions, such as ob_start (), ob_end_flush (), ob_get_contents ()

The following is the encoding section.

1. the initialization function is generally used to set the page cache path and the cache file naming format, which can be customized according to your preferences. The ID used here is the encrypted $ _ SERVER [REQUEST_URI] parameter. The last if judgment in this function is: if the cache period is not passed, the cached file will be loaded; otherwise, the source file will be loaded.

Copy codeThe code is as follows:
Function page_init ()
{
$ Url = $ _ SERVER ['request _ URI ']; // suburl. this parameter is generally unique.
$ Pageid = md5 ($ url );
$ Dir = str_replace ('/', '_', substr ($ _ SERVER ['script _ name'], 1,-4 ));
// Directory naming, such as exp_index
If (! File_exists ($ pd = PAGE_PATH. $ dir. '/') @ mkdir ($ pd, 0777) or die ("$ pd directory creation failed ");
// Such as cache/page/exp_index/
Define('page_file', paipd.w.pageid.'.html ');
// Such as cache/page/exp_index/cc8ef22b405566745ed21305dd248f0e.html
$ Contents = file_get_contents (PAGE_FILE); // Read

If ($ contents & substr ($ contents, 13, 10)> time () // corresponds to the custom header added to the page_cache () function
{
Echo substr ($ contents, 27 );
Exit (0 );
}
Return true;
}


2. page cache function. here we use a tip: add a header information in the content of the cached file-expiration time, therefore, you only need to compare the expiration time in the header with the current time (in the page_init () function) to determine whether the cache has expired.
Copy codeThe code is as follows:
Function page_cache ($ ttl = 0)
{
$ Ttl = $ ttl? $ Ttl: PAGE_TTL; // The cache time. the default value is 3600 s.
$ Contents = ob_get_contents (); // Obtain the content from the cache
$ Contents =" \ N ". $ contents;
// Add the custom header: Expiration Time = generation time + cache time
File_put_contents (PAGE_FILE, $ contents); // write to the cache file
Ob_end_flush (); // release the cache
}

3. use the function. Note that the two functions are executed sequentially, and do not forget ob_start ()
Copy codeThe code is as follows:
Page_init (); // page cache initialization
Ob_start (); // enable cache

... // Code segment

Page_cache (60); // usually the last row

?>

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.