Simple page buffering technology

Source: Internet
Author: User
Tags flock php3 file
The simple page buffering technology, read the simple page buffering technology, author: limodou preface actually said it is a technology, maybe not a real technology. This is just a page processing method I have come up with. of course, it may be consistent with others' ideas. But I still want to give it a nice name. So what is the page buffer I refer to here? <LINKhref = "http://www.php100.c author: limodou

Preface
It is actually a technology, maybe not a real technology. This is just a page processing method I have come up with. of course, it may be consistent with others' ideas. But I still want to give it a nice name. So what is the page buffer I refer to here? This means to save the dynamically generated page for the next use. In this way, the next access may not need to be dynamically generated. Just like providing a cache. On my website, maybe the same is true for your website. using technologies such as templates, the pages you see are dynamically generated. However, if a page is like this for you and others, it will not change for a period of time, isn't it better to directly return the result generated last time to the user who accessed the next time? This reduces the generation time and is more efficient. I think with the development of the website, the speed and efficiency issues still need to be considered. Here I will provide my implementation, hoping to help you. There is no specific implementation.

Usage conditions
Is it best to use all webpages? I don't want it, and it's impossible. The reason for buffering is that the content of the next access and the last access may be exactly the same. Therefore, it is not suitable for frequently changing pages. For example, it is not appropriate to display the count information on the page. In addition, if your dynamic page output is not first output to the variable, but directly returned to the user, such as echo, print, readfile, I personally don't think it can be done yet. Because I cannot get the output result and save it to the file (I thought for a long time that I didn't come up with anything to cut the output directly, redirects to a file ). The appropriate processing of dynamic pages is that the output results can be put into a string. Therefore, the conditions are as follows:
The page remains unchanged.
The processing results of dynamic pages can be stored in strings.
In this way, it is good to use the template class to process dynamic pages. You can set replaceable variables in the template, replace the variables in the template according to the actual values, and put the results in strings for output, the processing of this template class is very suitable for storing processed pages. Of course, it is feasible to generate output results without using a template class or using string processing. We will not discuss how to do this.

Implementation
As mentioned above, it is not a real implementation, but an implementation idea.

Process:

Generate a buffer file name based on the access requirements.
Check whether the file name exists. if the file does not exist, a dynamic page is generated, the page is saved, and the result is output at the same time. if the file exists, step 1 is executed to count the file modification time, and the modification time of files related to dynamic page generation compared 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, if the dynamic result may change, the dynamic page result is regenerated, saved to the file, and the output result ends. otherwise, step 2 is executed.
Indicates that the buffer file is up to date, and the buffer file is output directly.
This is my processing. You can create a temporary directory or use a database to store the cached files. If the data base is used, the method for determining whether the file is up-to-date should also be changed. for example, you can add the generation time field in the database and compare the modification time between this time field and other files. The method is self-developed.

My specific implementation example
To help you have a perceptual knowledge, I will provide a file-based processing method on my homepage. Only the primary processing generation
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 an 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 // process the template 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, I would like to introduce my directory structure:

/--- Bin/execution program directory
| -- Content. php3: The program used to process file display
| -- Template/directory used to store template files
| --- Content. ihtml template file
|-Docs/data file
|-Tmp/stores cached files

The content. php3 file is used to process dynamic pages. You can use content. php3? Page = id to read a data file. With
As long as you know that each data file has a different ID number, the content. php3? Page = ID
You can uniquely identify a data file.

Line 1-3 generates a temporary file name. Change '? Replace ',' & ''_'.
Row 3: determines whether the temporary file name exists. If yes, execute line 18-22 and end.
Line 6-13: determine the file modification time related to the dynamic page generation and the temporary file update, and set the regenerate flag. Use it here
Filemtime () to get the last modification time.
Line 24-41: use the template class to generate dynamic results and place them in variables. For more information about template processing, see templates and PHPLIB processing methods.
Article.
Line 43-50 generates temporary files. The file is locked to avoid write conflicts.
51st rows, output results.

This is my solution. you can modify it on your own.

Caching is a meaningful technology that increases access speeds and reduces system consumption. However, there may be a variety of methods.
By play.


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.