PHP about simple page buffering technology

Source: Internet
Author: User
Tags flock variables readfile
The page actually says it is a technology, maybe it can't be said to be a real technology. This is just the way I want to deal with the page, of course, with other people's ideas may be consistent. But I still want to give it a nice name. So what is the page buffer I'm referring to here? is to save a dynamically generated page for the next use. So the next time you access it, it may not need to be dynamically generated. It's like providing a cache. On my site, perhaps your site is the same, using technology such as templates, so that users see the page is dynamically generated. But a page for you is this, for others may still be, that is, in a period of time will not change, if the last generated results directly to the next access to the user is not better? Reduces the generation time, the efficiency is some higher. I think with the development of the site, speed and efficiency issues still need to be considered. Here I give my realization, hope to be helpful to everybody. Just a thought, no concrete implementation.

Use conditions
Is it best to use all the pages? I think it's not necessary and impossible. Buffering is because the next access may be exactly the same as the previous access. So it's not appropriate for pages that are constantly changing. For example, the page to display the count information is not appropriate. Also is if your dynamic page output, not first output to the variable, but directly back to the user, such as the use of Echo,print, ReadFile and so on output, I personally think that is still not done. Because you can't get the output, save it to a file (anyway, I was thinking for a long time not figuring out what to cut off the output and redirect it to the file). Then the more appropriate dynamic page processing is: The output should be able to put into a string. So the terms of use are:

The page basically doesn't change

The results of dynamic page processing can be stored in a string

It would be nice to use a template class to process a dynamic page. The processing of a template class is ideal for saving a processed page by setting replaceable variables in the template, and then replacing the variables in the corresponding template with the actual values, as well as putting the results in a string for output. Of course, it is also possible to generate output results from the processing of strings without using template classes. As for how to do it, we will not discuss it.

Realize
As mentioned earlier, not a real implementation, but a realization of the idea.

Processing process:

Generate a buffer file name based on Access requirements

To see if the file name exists, if it does not exist, generate a dynamic page, save the page, output the results, and then finish the 3rd step

The time that the statistics file was modified, and how long the files related to the dynamic page generation were modified

Compare buffer file modification time and other page modification time, if other page modification time is larger than the buffer file modification time, think dynamic result may change, then regenerate dynamic page result, save to file, and output result, end; otherwise perform step 5th

Description buffer file is up to date, direct output buffer file

That's what I'm dealing with. As for how to save a buffer file, you can build a temporary directory or use database processing. If the database is used to determine whether the latest way the file should also be changed, such as in the database to increase the generation time field, compare this time field and other files modified time. Method everyone wants to be himself.
 
Examples of my specific implementations
To help everyone have a perceptual knowledge, here I give a file-based approach implemented on my home page. Only the main processing code is 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);
$ctmp=filemtime (basename ($php_self));
One $cflag=$cflag | ($dtmp < $ctmp);
$ttmp=filemtime ("template/content.ihtml");
$cflag=$cflag | ($dtmp < $ttmp);
14}
Else
$cflag=true;
17
if (!$cflag)//Use Existing Files
19 {
ReadFile ($tmpfile);
Exit;
22}
23
24//Create a new file
Include "TEMPLATE.CLASS.PHP3";
26
$fp=fopen ($incfile, "R");
$content=fread ($fp, FileSize ($incfile));
Fclose ($FP);
30
31//below template processing
$t = new Template ("Template", "keep");
33
$t->set_file ("ContentFile", "content.ihtml");
35
$t->set_var (
Panax Notoginseng Array (
"Content" =>$content
39));
40
$t->parse ("Outputcontent", "contentfile");
42
$fp=fopen ($tmpfile, "w");
if ($FP)
45 {
Flock ($FP, 3);
Fwrite ($FP, $t->get_var ("outputcontent"));
Flock ($FP, 1);
Fclose ($FP);
50}
Wuyi $t->p ("outputcontent");
?>
Let me introduce you to my directory structure:
/---Directory of bin/executing programs
| |--CONTENT.PHP3 program for working with file display
| |--template/the directory used to store template files
| | |---content.ihtml template file
|-docs/Data files
|-tmp/Storage Buffer File
 
ONTENT.PHP3 files are used to process dynamic pages. Users can read a data file through the Content.php3?page=id number. I will not say the specific method, as long as you know each data file has a different ID number, so that the way the Content.php3?page=id number can uniquely identify a data file.

第1-3 line to generate a temporary file name. Replace the '? ', ' & ' characters with ' _ '.
Line 4th, to determine whether the temporary file name exists, and if so, execute the 第18-22 line and end it.
第6-13 the row to determine which update of the file modification time associated with generating the dynamic page and the temporary file, and set the rebuild flag. Use the function filemtime () here to get the last modification time.
第24-41 rows, using the template class to generate dynamic results, placed in the variable. About the template processing can refer to the "template, Phplib processing method" article.
第43-50 line to generate temporary files. The file is lock processed here as a write conflict.
Line 51st, output results.

This is my deal, you can change it by yourself.

Buffering is a meaningful technology that can improve access speed and reduce system consumption. But there may be a variety of ways in which we can play freely.

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.