PHP中简单的页面缓冲技术
页面
其实说它为技术,也许不能说是真正的技术。这只不过是我自已想出来的页面处理的方法,当然与别人的想法可能是一致的。不过我还是想给它一个好听的名字。那么我这里所指的页面缓冲是什么呢?就是指将动态生成的页面保存起来,供下一次的使用。这样下一次访问它可能就不需要动态生成了。就象提供了一个cache 一样。在我的网站上,也许你的网站也是如此,使用了象模板之类的技
术,这样用户所看到的页面就是动态生成的。但是一个页面对于你是这样,对于别人可能还是这样,即在一段时间内是不会变化的,如果将上次生成的结果直接返回给下一次访问的用户不是更好吗?减少了生成时间,效率要高一些。我想随着网站的发展,速度与效率问题还是要考虑的。这里我给出我的实现,希望对大家有所帮助。只是一个思路,没有具体的实现。
使用条件
是不是所有的网页最好都使用呢?我想不需要,而且也不可能。之所以能缓冲就是因为下一次访问与上一次访问的内容可能是完全一样的。所以对于经常变化的页面就不合适了。比如页面上要显示计数信息的就不太合适。还有就是如果你的动态页面输出时,没有先输出到变量中,而是直接返回给用户,如使用echo,print ,readfile之类的输出,我个人认为现在还作不到。因为无法将输出结
果得到,保存到文件中去(反正我是想了半天没有想出有什么可以将直将输出的东西截下来,重定向到文件中去)。那么比较适的动态页面的处理就是:输出结果应该可以放到一个字符串之中。所以使用条件就是:1.页面基本不会变化 2.动态页面的处理结果可以存放到字符串中。
这样使用模板类来处理动态页面就很好了。通过在模板中设置可替换的变量,然后根据实际的值替换相应的模板中的变量,同时可以将结果放到字符串中进行输出,这种模板类的处理非常适合保存处理后的页面。当然不使用模板类,也可以通过字符串的处理来生成输出结果也是可行的。至于怎么做就不讨论了。
实现
如前所述,不是一个真正的实现,而是一个实现的思路。
处理流程:
1.根据访问的要求,生成缓冲文件名
2.查看文件名是否存在,如果文件不存在,则生成动态页面,将页面保存,同时输出结果,结束;如果存在,则执行第3步
3.统计文件的修改时间,及与动态页面生成有关的文件的修改时间
4.比较缓冲文件的修改时间与其它页面的修改时间,如果其它页面修改时间大于缓冲文件修改时间,认为动态结果可能会发生变化,则重新生成动态页面结果,保存到文件中,且输出结果,结束;否则执行第5步
5.说明缓冲文件最新,则直接输出缓冲文件
这就是我的处理。至于缓冲文件如何保存,可以建一个临时目录也可以使用数据库处理。如果使用了数据库则判断文件是否最新的方式也应作变化,比如在数据库中增加生成时间字段,比较这个时间字段与其它文件的修改时间即可。方法大家自已想。
我的具体实现的例子
为了帮助大家有个感性认识,这里我给出在我的主页上实现的基于文件处理的方法。只有主要的处理代码,不完整。
----------------------------------------------------------------------
----------
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行,输出结果。
这就是我的处理,大家可以自行修改。
缓冲是一项有意义的技术,可以提高访问速度,减少系统消耗。不过方法可能有多种多样,大家可以自由发挥。

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

Golang and Template package: Create personalized user interface In modern software development, the user interface is often the most direct way for users to interact with the software. In order to provide a user interface that is easy to use and beautiful, developers need flexible tools to create and customize the user interface. In Golang, developers can use the Template package to achieve this goal. This article will introduce the basic usage of Golang and Template packages, and show how to create a personalized user interface through code examples.

Go language is an increasingly popular programming language with its concise syntax, efficient performance, and easy development. The Go language provides a powerful template engine - "text/template", but when using it, some people may encounter the "undefined:template.Must" error. The following is a method to solve this error. Import the correct package. When using the "text/template" template engine, you need to import "text/template

Data visualization through Golang's Template package. With the advent of the big data era, data visualization has become one of the important means of information processing and analysis. Data visualization can present data in a concise and intuitive way, helping people better understand and analyze data. In Golang, we can use the Template package to implement data visualization functions. This article will introduce how to use Golang's Template package to achieve data visualization and provide code examples. GolangTem

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_

Golang and the Template package: Building a powerful front-end development toolbox In today's software development, front-end development is becoming more and more important throughout the project. In order to provide an elegant and reliable front-end development solution, we can use the Golang language and its built-in Template package. This article will introduce how to use Golang and the Template package to build a powerful front-end development toolbox. 1. Golang: Efficient and easy-to-use programming language Golang is a modern

Use of the opsForValue() method in Redis 1. set(Kkey, Vvalue) adds a string type value, key is the key, and value is the value. redisTemplate.opsForValue().set("stringValue","bbb"); 2. get(Objectkey) gets the value corresponding to the key key. StringstringValue=redisTemplate.opsForValue().get("key")3. append(Kkey,St
