Home Backend Development PHP Tutorial PHP中简单的页面缓冲技术_PHP

PHP中简单的页面缓冲技术_PHP

Jun 01, 2016 pm 12:34 PM
use dynamic deal with technology document time generate Simple output page

 其实说它为技术,也许不能说是真正的技术。这只不过是我自已想出来的页面处理的方法,当然与别人的想法可能是一致的。不过我还是想给它一个好听的名字。那么我这里所指的页面缓冲是什么呢?就是指将动态生成的页面保存起来,供下一次的使用。这样下一次访问它可能就不需要动态生成了。就象提供了一个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行,输出结果。

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

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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 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)

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

Convert VirtualBox fixed disk to dynamic disk and vice versa Convert VirtualBox fixed disk to dynamic disk and vice versa Mar 25, 2024 am 09:36 AM

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

DualBEV: significantly surpassing BEVFormer and BEVDet4D, open the book! DualBEV: significantly surpassing BEVFormer and BEVDet4D, open the book! Mar 21, 2024 pm 05:21 PM

This paper explores the problem of accurately detecting objects from different viewing angles (such as perspective and bird's-eye view) in autonomous driving, especially how to effectively transform features from perspective (PV) to bird's-eye view (BEV) space. Transformation is implemented via the Visual Transformation (VT) module. Existing methods are broadly divided into two strategies: 2D to 3D and 3D to 2D conversion. 2D-to-3D methods improve dense 2D features by predicting depth probabilities, but the inherent uncertainty of depth predictions, especially in distant regions, may introduce inaccuracies. While 3D to 2D methods usually use 3D queries to sample 2D features and learn the attention weights of the correspondence between 3D and 2D features through a Transformer, which increases the computational and deployment time.

How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? Mar 24, 2024 pm 01:31 PM

Xiaohongshu, a platform full of life and knowledge sharing, allows more and more creators to express their opinions freely. In order to get more attention and likes on Xiaohongshu, in addition to the quality of content, the time of publishing works is also crucial. So, how to set the time for Xiaohongshu to publish works? 1. How to set the time for publishing works on Xiaohongshu? 1. Understand the active time of users. First, it is necessary to clarify the active time of Xiaohongshu users. Generally speaking, 8 pm to 10 pm and weekend afternoons are the times when user activity is high. However, this time period will also vary depending on factors such as audience group and geography. Therefore, in order to better grasp the active period of users, it is recommended to conduct a more detailed analysis of the behavioral habits of different groups. By understanding users’ lives

See all articles