Home php教程 php手册 简单的页面缓冲技术(二)

简单的页面缓冲技术(二)

Jun 21, 2016 am 09:10 AM
content filemtime quot tmpfile

页面

我的具体实现的例子
  为了帮助大家有个感性认识,这里我给出在我的主页上实现的基于文件处理的方法。只有主要的处理代码,不完整。

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 10 $ctmp=filemtime(basename($PHP_SELF));
11 $cflag=$cflag | ($dtmp 12 $ttmp=filemtime("template/content.ihtml");
13 $cflag=$cflag | ($dtmp 14 }
15 else
16 $cflag=true;
17
18 if(!$cflag) //使用存在的文件
19 {
20 readfile($tmpfile);
21 exit;
22 }
23
24 //创建新的文件
25 include "template.class.php3";
26
27 $fp=fopen($incfile, "r");
28 $content=fread($fp, filesize($incfile));
29 fclose($fp);
30
31 //下面进行模版处理
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");
?>


  先向大家介绍一下我的目录结构:

  /---bin/ 执行程序目录
  | |--content.php3 用于处理文件显示的程序
  | |--template/ 用于存放模板文件的目录
  | |---content.ihtml 模板文件
  |-docs/ 数据文件
  |-tmp/ 存放缓冲文件

  content.php3文件用来处理动态页面。用户可以通过content.php3?page=id号来读出一个数据文件。具体方法我就不说了,大家只要知道每个数据文件都有一个不同的id号,这样content.php3?page=id号的方式就可以唯一标识一个数据文件。

第1-3行,生成临时文件名。将'?','&'等字符替换成'_'。
第4行,判断临时文件名是否存在,如果有则执行第18-22行,并结束。
第6-13行,判断与生成动态页面有关的文件修改时间与临时文件哪个更新,设置重新生成标志。在这里使用  filemtime()来得到最后修改时间。
第24-41行,利用模板类生成动态结果,放在变量中。关于模板的处理可以参考《模板,PHPLIB处理方式》一文。
第43-50行,生成临时文件。此处对文件进行了加锁处理,以象写冲突。
第51行,输出结果。

  这就是我的处理,大家可以自行修改。

  缓冲是一项有意义的技术,可以提高访问速度,减少系统消耗。不过方法可能有多种多样,大家可以自由发挥。

本站注:看了这篇文章,竟发现本文作者的思路和本站的静态文章更新的实现原理是一样的。不同的是,本站的文章是每四天定期更新的。如果有兴趣的朋友可以通过页面下方的"view source"链接来查看具体实现的代码。如果有什么疑问和好的思想,欢迎到论坛共同讨论。



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP function introduction—filemtime(): Get the last modification time of a file PHP function introduction—filemtime(): Get the last modification time of a file Jul 24, 2023 pm 05:41 PM

PHP function introduction—filemtime(): Get the last modification time of a file Overview: In PHP, filemtime() is a very commonly used function, used to get the last modification time of a file. Through this function, we can get the last modification timestamp of the file to facilitate the operation and processing of the file. This article will introduce how to use the filemtime() function and provide code examples to help readers better understand and use this function. Function syntax: intfilemtime

Use the PHP function 'filemtime' to return the modification time of a file Use the PHP function 'filemtime' to return the modification time of a file Jul 24, 2023 am 10:01 AM

The PHP function "filemtime" can be used to get the last modification time of a file. Its use is very simple, just pass in the file path as a parameter, and the function will return a timestamp indicating the last modification time of the file. Next, I will introduce how to use this function and some code examples. In PHP, we can use the "filemtime" function in the following way: $file_path='path/to/file.txt';//File path

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

PHP function introduction: is_file() function PHP function introduction: is_file() function Nov 04, 2023 am 09:11 AM

PHP function introduction: is_file() function In PHP programming, the is_file() function is a very useful function. It is used to determine whether a path or file exists and is an ordinary file. In this article, we will introduce how to use the is_file() function and provide some specific code examples. First, let's take a look at the syntax of the is_file() function: boolis_file(string$filename)is_

PHP creates a temporary file PHP creates a temporary file Mar 21, 2024 am 11:57 AM

This article will explain in detail about creating a temporary file in PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Introduction to creating temporary files with PHP In some cases, PHP needs to create temporary files to store or process data. Temporary files are files that are temporarily stored in the system file system and are usually automatically deleted after the script is executed. Step 1. Create a temporary file handle. Use the tmpfile() function to create a temporary file handle. This function returns a stream pointing to a temporary file that can be used to write and read data. $file=tmpfile();2. Write data Use the fwrite() function to write data to the temporary file.

PHP function introduction—tmpfile(): Create a temporary file PHP function introduction—tmpfile(): Create a temporary file Jul 25, 2023 pm 11:21 PM

PHP function introduction—tmpfile(): Creating a temporary file In PHP programming, processing files is a very common operation. In some cases, we need to temporarily create a file to perform certain processing, but we do not want to save the file for a long time. At this time, you can use PHP's tmpfile() function to create a temporary file. This article will introduce to you the usage of the tmpfile() function and related precautions. The tmpfile() function is one of the PHP file system functions.

CSS content properties explained: content, counter, and quotes CSS content properties explained: content, counter, and quotes Oct 21, 2023 am 10:16 AM

Detailed explanation of CSS content attributes: content, counter and quotesCSS (cascading style sheets) is an integral part of front-end development. It can help us beautify web pages and enhance user experience. In CSS, there are some special properties that can be used to control the display of text content, including content, counter, and quotes. This article explains these properties in detail and provides specific code examples. 1. content attribute content attribute

Microsoft AI CEO says using open web content to train AI models isn\'t wrong Microsoft AI CEO says using open web content to train AI models isn\'t wrong Jun 30, 2024 pm 10:59 PM

In an interview with CNBC's Andrew Ross Sorkin, Microsoft AI CEO Mustafa Suleymanexpressed his understanding of web copyright law. Suleyman says that content that is accessible on the open web is "freeware," and anyone can copy it, reproduc

See all articles