Introduction to php page cache ob series functions_PHP tutorial

WBOY
Release: 2016-07-13 09:44:12
Original
757 people have browsed it

Introduction to php page cache ob series functions

I have been exposed to the page cache of phpcms in the past few days and have some feelings. I won’t go into details about its benefits. It is generally used in pages with a lot of database queries. It is not suitable for pages that insert, modify, and delete

PHP page caching mainly uses the ob series of functions, such as ob_start(), ob_end_flush(), ob_get_contents()

The following is the encoding part.

1. Initialization function, usually setting the page cache path, cache file naming format, etc., which can be customized according to personal preferences. The identification ID used here is the encrypted $_SERVER[REQUEST_URI] parameter. There is an if judgment at the end of this function: if the cache period has not passed, load the cache file, otherwise load the source file.

Function page_init()

 {

 $url = $_SERVER['REQUEST_URI'];//Sub-url, this parameter is generally unique

$pageid = md5($url);

 $dir = str_replace('/','_',substr($_SERVER['script_NAME'],1,-4));

 //Directory naming method, 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',$pd.$pageid.'.html');

// Such as cache/page/exp_index/cc8ef22b405566745ed21305dd248f0e.html

 $contents = file_get_contents(PAGE_FILE);//Read out

 if($contents && substr($contents, 13, 10) > time() )//corresponds to the custom header added in the page_cache() function

 {

echo substr($contents, 27);

exit(0);

 }

return true;

 }

2. Page caching function, a trick is used here: add a header information - expiration time - to the content of the cache file, so each time you only need to compare the expiration time in the header with the current time ( Perform it in the page_init() function) to determine whether the cache has expired.

Function page_cache($ttl = 0)

 {

 $ttl = $ttl ? $ttl : PAGE_TTL;//Cache time, default 3600s

 $contents = ob_get_contents();//Get content from cache

 $contents = " ".$contents;

//Add custom header: expiration time = generation time cache time

 file_put_contents(PAGE_FILE, $contents);//Write to cache file

ob_end_flush();//Release cache

 }

 3. When using functions, please note that these two functions are executed in sequence, and don’t forget ob_start()

 

page_init();//Page cache initialization

ob_start();//Enable caching

 ...//Code segment

page_cache(60);//usually the last line

 ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1049988.htmlTechArticleIntroduction to the php page cache ob series functions. I have been exposed to the page cache of phpcms in the past few days, and I have some feelings. Not much to say about its benefits. It is generally used in pages with a lot of database queries. For insertion...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template