简单的页面缓冲技术(二)
页面
我的具体实现的例子
为了帮助大家有个感性认识,这里我给出在我的主页上实现的基于文件处理的方法。只有主要的处理代码,不完整。
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"链接来查看具体实现的代码。如果有什么疑问和好的思想,欢迎到论坛共同讨论。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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_

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(): 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.

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

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
