使用ob_start缓冲输出做简单缓存
PHP ob_start()函数是一个功能强大的函数,可以帮助我们处理许多问题。
Output Control 函数可以让你自由控制脚本中数据的输出。它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况。输出控制函数不对使用header() 或setcookie(), 发送的文件头信息产生影响,只对那些类似于echo() 和PHP 代码的数据块有作用。
所有对header()函数有了解的人都知道,这个函数会发送一段文件头给浏览器,但是如果在使用这个函数之前已经有了任何输出(包括空输出,比如空格,回车和换行)就会提示出错。如果我们去掉第一行的ob_start(),再执行此程序,我们会发现得到了一条错误提示:"Header had all ready send by"!但是加上ob_start,就不会提示出错,原因是当打开了缓冲区,echo后面的字符不会输出到浏览器,而是保留在服务器,直到你使用flush或者ob_end_flush才会输出,所以并不会有任何文件头输出的错误!
下面介绍下如何使用ob_start做简单缓存。
<?php $time1 = microtime(true); for($i = 0;$i < 9999;$i++) { //echo $i.'<br />'; } echo "<br />"; $time2 = microtime(true); echo $time2 -$time1; // 输出 0.0010678768158 ?>
没做缓存的时候,运行时间为 0.0010678768158。
1. 简单缓存
<?php $time1 = microtime(true); $cache_file = "file.txt"; if(file_exists($cache_file)) { $info = file_get_contents($cache_file); echo $info; $time2 = microtime(true); echo $time2 -$time1; exit(); } ob_start(); for($i = 0;$i < 9999;$i++) { //echo $i; } echo "<br />"; $info = ob_get_contents(); file_put_contents($cache_file ,$info); $time2 = microtime(true); echo $time2 -$time1; // 输出 0.00075888633728 ?>
没做缓存耗时 0.001秒,做了简单缓存则为 0.0007秒,缓存后速度稍有提升。
2. 进一步缓存
在前面缓存的基础上进一行加深。大家都知道,js文件不仅不耗费服务器的资源,同时会被下载到客户端,秩序下载一次,之后就不消耗带宽了,缺点就是不可以被搜索引擎抓到包,但是对于办公系统来说,是一个非常好的选择。
<?php $time1 = microtime(true); function htmltojs($str) { $re=''; $str=str_replace('\','\\',$str); $str=str_replace("'","'",$str); $str=str_replace('"','"',$str); $str=str_replace('t','',$str); $str= split("rn",$str); //已分割成数组 for($i=0;$i < count($str); $i++) { $re.="document.writeln("".$str[$i]."");rn"; //加上js输出 } $re = str_replace(""); document.writeln("","",$re); return $re; } $cache_file = "file.js"; if(file_exists($cache_file)) { $info = file_get_contents($cache_file); show_script($cache_file); $time2 = microtime(true); echo $time2 -$time1; exit(); } ob_start(); for($i = 0;$i < 9999;$i++) { //echo $i; } echo "<br />"; $info = ob_get_contents(); $info = htmltojs($info); file_put_contents($cache_file ,$info); $time2 = microtime(true); echo $time2 -$time1; ?>
只是简单地提供一个缓存的思路。

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
