PHP uses caching to generate static pages

Source: Internet
Author: User

Http://www.cnblogs.com/lh460795/archive/2013/04/06/3003105.html

A stress test can be done at Apache/bin/ab.exe, which simulates multiple people and accesses a page concurrently.

The basic usage

Ab.exe–n 10000–c 10

-N indicates how many times the request

-C indicates how many people

If you want to test PHP's own caching mechanism, you need to configure it.

php.ini file

Display_errors=on

Output_buffering=off

error_reproting= Setting the error level

Looking at a piece of code, when using the cache, you can display text before sending the file header.

<?php

echo "YYY";

Header ("Content-type:text/htm;charset=utf-8");

echo "Hello";

?>

Several functions of PHP cache control:

1//php Cache control several functions: 2//Open cache [via PHP.ini, can also be on page Ob_start ()] 3 ob_start (); 4 echo "yyy"; 5 Header ("Content-type:text/htm;charset=utf-8"); 6 echo "Hello"; The 7//ob_clean function can empty the contents of the OutputBuffer. 8//ob_clean (); 9//ob_end_clean is to turn off the OB cache while emptying.//ob_clean ();//ob_end_flush () function is the memory output of the OB cache and closes ob12//ob_end_flush ();//ob_end_ The flush () function is the memory output of the OB cache, the//ob_flush () function is the output OB content, and is emptied, but not closed. Ob_flush ();         echo "KKK";//=>ob cache.//header ("Content-type:text/htm;charset=utf-8");//ob_get_contents () can get output_buffering content.//$contents =ob_get_ Contents ();//file_put_contents ("D:/log.text", $contents);

Let's look at an example, using caching technology, "if the cache file is not stored for more than 30 seconds, the cache file is taken directly":

 1 <?php 2 $id =$_get[' id ']; 3 $filename = "Static_id_". $id. ". HTML "; 4 $status =filemtime ($filename) +30>time ();//Determine if the file creation and modification time is longer than 30 seconds 5 if (File_exists ($fi                 Lename) && $status) {6 $str =file_get_contents ($filename); 7 echo $str; 8 }else{9 require_once "SqlHelper.class.php"; $sqlHelper =new Sqlhelp ER (); one $arr = $sqlHelper->execute_dql2 ("select * from News1 WHERE id= $id"); (Empty ($arr))                         {echo "Data is empty";}else{15/*** Cache starts ***/16                         Ob_start ();//The contents below will be stored in the buffer, and the contents will be stored in the buffer. Echo $arr [0][' Tile '];18 echo "<br/>", Echo $arr [0][' content '];20 $content = Ob_get_con            Tents ();//get content from the cache 21             Ob_end_clean ();//close cache and empty 22/*** cache End ***/23 File_put_contents ($                 FileName, $content), Echo $content 25}26}27 ?>

PHP uses caching to generate static pages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.