Home  >  Article  >  Backend Development  >  Simple page buffering technology_PHP tutorial

Simple page buffering technology_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:00:38766browse

Author: limodou

Foreword
In fact, if we call it technology, it may not be called a real technology. This is just my own way of processing the page. Of course, it may be consistent with other people's ideas. But I still want to give it a nice name. So what is page buffering I am referring to here? It means to save the dynamically generated page for next use. This way the next time you access it, you may not need to dynamically generate it. It's like providing a cache. On my website, and maybe yours too, technologies like templates are used so that the pages the user sees are dynamically generated. But a page that is like this to you may still be like this to others, that is, it will not change for a period of time. Wouldn't it be better if the results generated last time were directly returned to the user who visits next time? The generation time is reduced and the efficiency is higher. I think as the website develops, issues of speed and efficiency still need to be considered. Here I give my implementation, I hope it will be helpful to everyone. It's just an idea without concrete implementation.

Conditions of use
Is it best to use it on all web pages? I don't think it's necessary, and it's impossible. The reason why it can be buffered is that the content of the next access and the previous access may be exactly the same. So it is not suitable for pages that change frequently. For example, it is not appropriate to display counting information on the page. Also, if your dynamic page is output without first outputting it to a variable, but directly returning it to the user, such as using echo, print, readfile and other outputs, I personally think it is not possible yet. Because the output results cannot be obtained and saved to a file (anyway, I have been thinking about it for a long time and haven't figured out anything that can cut off the output and redirect it to a file). Then the more appropriate processing of dynamic pages is: the output results should be placed in a string. So the conditions for use are:
The page will basically not change
The processing results of dynamic pages can be stored in strings
In this way, it is good to use template classes to process dynamic pages. By setting replaceable variables in the template, and then replacing the variables in the corresponding template according to the actual values, the results can be put into strings for output. This kind of template class processing is very suitable for saving the processed page. Of course, it is also feasible to generate output results through string processing without using template classes. How to do it will not be discussed.

Implementation
As mentioned before, it is not a real implementation, but an idea of ​​implementation.

Processing flow:

Generate buffer file name according to access requirements
Check whether the file name exists. If the file does not exist, generate a dynamic page, save the page, and output the result at the same time , end; if it exists, perform step 3 to count the modification time of the file, and compare the modification time of the files related to dynamic page generation with the modification time of the buffer file and the modification time of other pages. If the modification time of other pages is greater than the modification time of the buffer file Time, if you think the dynamic results may change, regenerate the dynamic page results, save them to the file, and output the results, and end; otherwise, perform step 5
to indicate that the buffer file is the latest, then directly output the buffer file
This That's what I deal with. As for how to save buffer files, you can create a temporary directory or use database processing. If a database is used, the method of determining whether a file is the latest should also be changed, such as adding a generation time field to the database and comparing this time field with the modification times of other files. Everyone thinks of their own method.

My specific implementation example
In order to help everyone have a perceptual understanding, here I give the file-based processing method implemented on my homepage. Only main processing code
, incomplete.

-------------------------------------------------- ------------------------------------
1 $tmpfile=" ../tmp/".basename($REQUEST_URI);
2 $tmpfile=str_replace("?", "_", $tmpfile);
3 $tmpfile=str_replace("&", "_" , $tmpfile);
4 if(file_exists($tmpfile))
5 {
6 $cflag=false;
7 $dtmp=filemtime($tmpfile);
8 $itmp =filemtime($incfile);
9 $cflag=$cflag | ($dtmp < $itmp);
10 $ctmp=filemtime(basename($PHP_SELF));
11 $cflag=$ cflag | ($dtmp < $ctmp);
12 $ttmp=filemtime("template/content.ihtml");
13 $cflag=$cflag | ($dtmp < $ttmp);
14 }
15 else
16 $cflag=true;
17
18 if(!$cflag) //Use existing file
19 {
20 readfile($tmpfile );
21 exit;
22 }
23
24 //Create a new file
25 include "template.class.php3";
26
27 $fp =fopen($incfile, "r");
28 $content=fread($fp, filesize($incfile));
29 fclose($fp);
30
31 // Template processing is performed below
32 $t = new Template("template", "keep");
33
34 $t->set_file("contentfile", "content.ihtml");
35
36 $t->set_var(
37 array(
38 "content"=>$content
39 ));
40
41 $t- >parse("outputcontent","contentfile");
42
43 $fp=fopen($tmpfile, "w");
44 if($fp)
45 {
46 flock($fp, 3);
47 fwrite($fp, $t->get_var("outputcontent"));
48 flock($fp, 1);
49 fclose( $fp);
50 }
51 $t->p("outputcontent");
?>
---------------- -------------------------------------------------- ---------------
First, let me introduce to you my directory structure:

/---bin/ Execution program directory
| |--content .php3 Program used to process file display
| |--template/ Directory for storing template files
| |---content.ihtml Template file
|-docs/ Data file
|-tmp/ stores buffer files

The content.php3 file is used to process dynamic pages. Users can read a data file through content.php3?page=id number. I won't go into the specific method
. As long as you know that each data file has a different ID number, the method content.php3?page=id number
can uniquely identify a data file.

Lines 1-3, generate temporary file names. Replace '?', '&' and other characters with '_'.
Line 4, determine whether the temporary file name exists, if so, execute lines 18-22 and end.
Line 6-13, determine whether the file modification time and the temporary file related to generating dynamic pages are updated, and set the regeneration flag. Use
filemtime() here to get the last modification time.
Lines 24-41 use template classes to generate dynamic results and place them in variables. Regarding the processing of templates, you can refer to the article "Template, PHPLIB Processing Method"
 .
Lines 43-50, generate temporary files. The file is locked here to prevent write conflicts.
Line 51, output the result.

This is how I handled it, you can modify it yourself.

Buffering is a meaningful technology that can improve access speed and reduce system consumption. However, there may be many different methods, and everyone can
use them as they please.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317027.htmlTechArticleAuthor: limodou The preface actually calls it technology, but it may not be said to be a real technology. This is just my own way of processing the page. Of course, it may be consistent with other people’s ideas...
Statement:
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