Gz_ Cache Technology 2

Source: Internet
Author: User
Tags php foreach

I. Review of the Content

To implement a static approach:

True static: Use the OB cache mode, replace with template,

Ob_start (); Open o B Cache

Ob_clean (); Clears the data inside the O B cache and does not close the OB cache.

Ob_end_clean ();//Clears the data inside the OB cache and closes the OB cache

Ob_flush ();//The data inside the OB cache is moved (refreshed) to the program cache without closing the OB cache.

Ob_end_flush ();//The data inside the OB cache is moved to the program cache and the OB cache is closed.

ob_get_contents (); Gets the content inside the OB cache.

In determining the cache validity period: generated static page last modified timestamp + cache time > current timestamp

Filemtime ($filename) +300>time ();

Ab.exe

Ab.exe–n the total number of requests to the-C concurrency test address.

MPM: The Multi-path processing module (Apache handles concurrency) is primarily a pre-derived model, a worker model, and a Winnt model.

View the current Apache Mpm:httpd.exe–l

In the program cache, store the header information first, then the main information of the page. A warning error occurs when the principal information is stored and the header information is stored (modified).

Second, how to configure the cache:

Cache some data that is not very frequent and caches it locally . The next time the site is requested, it is removed from the local area, without having to be removed from the server.

Basic diagram:

1, the principle of caching:

First Access header information:

Second visit:

2. How to configure the cache

(1) Open the main configuration file (httpd.conf) of Apache to turn on the cache module

Open Apache's expires extension, take advantage of this extension to control the image, css,html and other files to control whether the cache cache, and the cache declaration period.

(2) configuration option settings

Expiresactive on//Turn on cache settings

Specific cache rules are set for file types.

ExpiresDefault "<base> [plus] {<num> <type>}*"//Default settings
Expiresbytype type/encoding "<base> [plus] {<num> <type>}*"//set for different file types.

(3) The specific configuration steps:

To test: Create a new virtual host:

Think: If we want to configure the cache cycle for JS files, if you set the file type.

You can introduce a file in the page, such as:

Refresh the page through the browser to view the header information:

Expiresbytype application/javascript "Access plus ten days"

3, you can target some files, do not let him cache, always get the content from the server.

Example: GIF files are not cached.

Ideas, open the Apache master configuration file, in the header module, configure this option: Cache-control:no-store,must-revalidate,

Specific steps:

(1) Open the header module.

(2) Specific configuration: The idea is the file for GIF format.

In the virtual host, the following configuration:

(3) Effect:

Three, configuration compression

How to view the compression formats supported by your browser: You can view the compression format supported by the browser by requesting the Accept-encoding option in the header information.

such as common gzip compression, deflate compression, compress compression, and Google, Chrome are pushing SDCN compression.

Specific configuration steps:

(1) In Apache main configuration file, the compression configuration is turned on, Apache default is deflate compression.

(2) Inside the virtual host configuration.

(3) Start testing:

The length of the file itself:

Length before compression:

Length after compression:

Iv. complete a case of a true static

(Through the News list, click the news details, jump to the specific news page, the specific news page to complete the true static)

Schematic diagram:

Ideas: There are several main pages.

Addnews.html: News add page (with form)

action.php: News processing page (e.g. Add, edit, delete)

index.php: News List Page

Newsinfo: News Details page.

(1) Create a new news table:

(2) Create a new page to add news:

(3) Create a new action.php page to complete the storage

(4) Complete the homepage (List page)

<?php

$conn = mysql_connect ("localhost", ' root ', ' root ');

mysql_query ("Use PHP");

mysql_query ("Set names UTF8");

$sql = "Select Id,title from News";

$list =array ();

$res = mysql_query ($sql, $conn);

while ($row =mysql_fetch_assoc ($res)) {

$list []= $row;

}

?>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<title> New Page </title>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<meta name= "description" content= ""/>

<meta name= "keywords" content= ""/>

<script type= "Text/javascript" >

</script>

<style type= "Text/css" >

</style>

<body>

<table width= "border=" 1 ">

<tr><td> News Headlines </td><td> News details </td><td> Actions </td></tr>

<?php foreach ($list as $v) {?>

<tr><td><?php echo $v [' title ']?></td><td> News details </td><td> Operations </td> </tr>

<?php}?>

</table>

</body>

(5) On the list page, add a hyperlink to the news details

(6) Generate a static page for the news detail page.

<?php

Receive the ID passed over

$id = $_get[' id ']+0;

Name the corresponding static page

$filename = "news_id". $id. HTML ';

echo $filename;

Add Judgment

if (file_exists ($filename) && filemtime ($filename) +300>time ()) {

Echo ' 99999999 ';

include $filename; exit;

}

$conn = mysql_connect ("localhost", ' root ', ' root ');

mysql_query ("Use PHP");

mysql_query ("Set names UTF8");

$sql = "Select Title,content from news where id = $id";

$res = mysql_query ($sql, $conn);

$row =mysql_fetch_assoc ($res);

Ob_start ();

?>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<title> New Page </title>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<meta name= "description" content= ""/>

<meta name= "keywords" content= ""/>

<script type= "Text/javascript" >

</script>

<style type= "Text/css" >

</style>

<body>

<div><?php echo $row [' content ']?></div>

</body>

<?php

$str = Ob_get_contents ();

File_put_contents ($filename, $STR);

?>

Cons: The News details page, even if not modified, is regenerated every 5 minutes. Can be made, as long as the news has not been modified, the static page has been used, if the news changes will immediately regenerate a corresponding static page.

V. Use template replacement techniques to accomplish true static:

Ideas:

When adding news, generate the corresponding static page.

When you modify the news, regenerate the corresponding static page.

Make a link to a news listing page link directly to the generated static page.

Schematic diagram:

The specific steps;

(1) Create a new template file, tpl.html:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<title> New Page </title>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<meta name= "description" content= ""/>

<meta name= "keywords" content= ""/>

<script type= "Text/javascript" >

</script>

<style type= "Text/css" >

</style>

<body>

<div>%content%</div>

</body>

(2) Modify Action.php page

if ($act = = ' Add ') {

Receive form submit data

$title = $_post[' title '];

$content = $_post[' content '];

$sql = "INSERT INTO News (title,content) VALUES (' $title ', ' $content ')";

$res = mysql_query ($sql, $conn);

$id = mysql_insert_id ($conn);

if (! $id) {

Die ("Add failed");

}

Completing the Build static page

Name the corresponding static page

$filename = ' news_id '. $id. HTML ';

Open template file

$tpl _ph=fopen (' tpl.html ', ' R ');

Creates a corresponding static page.

$file _ph=fopen ($filename, ' w ');

Complete replacement

The feof () function returns whether the file pointer is to the end of the file, or True if it is returned.

while (!feof ($tpl _ph)) {

$row =fgets ($tpl _ph);//fgets () function reads a line of content

$row =str_replace ('%title% ', $title, $row);//str_replace () The function returns the result after the substitution

$row =str_replace ('%content% ', $content, $row);

Fwrite ($file _ph, $row);//writes the replaced content to the $filename file.

}

Fclose ($tpl _ph);//close file pointer

Fclose ($file _ph);//close file pointer

Echo ' Add success ';

echo "<a href= ' index.php ' > Back home </a>";

}

(3) Modify the News list page to change the details of the news link to the corresponding static page.

(4) The corresponding static page is regenerated when the news is modified.

(3) When the news is deleted, the corresponding static page is deleted. Unlink

Disadvantages:

(1) If the news is more, the generated static page is more, should adopt the hierarchical storage static page.

(2) has not yet reached the total static, we should let the first page to generate static pages, to achieve full station static.

(3) Add the path of the generated static page to the field in the database.

Vi. use of template replacement technology to complete the true static, further improve

A project has front desk and backstage, the whole station static is aimed at the front desk,

Case Planning:

(1) Copy the morning item (DEMO1) and move the file to the Admin directory.

Goal: Create the path to the generated static page and create a storage path for the filename field in the database

<?php

$act = isset ($_get[' act ') "Trim ($_get[' act ']):" ";

$conn = mysql_connect ("localhost", ' root ', ' root ');

mysql_query ("Use PHP");

mysql_query ("Set names UTF8");

Add News

if ($act = = ' Add ') {

Receive form submit data

$title = $_post[' title '];

$content = $_post[' content '];

$sql = "INSERT INTO News (title,content) VALUES (' $title ', ' $content ')";

$res = mysql_query ($sql, $conn);

$id = mysql_insert_id ($conn);

if (! $id) {

Die ("Add failed");

}

$dir = Date ("ym/d");

$file = "news_id". $id. HTML ';//indicates the name of the generated static file

$dir _path= ". /a/". $dir;

To determine if the $dir_path directory exists, create it if it does not exist.

if (!is_dir ($dir _path)) {

mkdir ($dir _path,0777,true);

}

$filename = $dir _path. ' /'. $file; The full path of the generated static file;

$filename _path= "./a/". $dir. '/'. $file;//The path of a static page that is stored in the FileName field of the database:

Open template file

$tpl _ph=fopen (' tpl.html ', ' R ');

Creates a corresponding static page.

$file _ph=fopen ($filename, ' w ');

Complete replacement

The feof () function returns whether the file pointer is to the end of the file, or True if it is returned.

while (!feof ($tpl _ph)) {

$row =fgets ($tpl _ph);//fgets () function reads a line of content

$row =str_replace ('%title% ', $title, $row);//str_replace () The function returns the result after the substitution

$row =str_replace ('%content% ', $content, $row);

Fwrite ($file _ph, $row);//writes the replaced content to the $filename file.

}

Fclose ($tpl _ph);//close file pointer

Fclose ($file _ph);//close file pointer

Store the path of the generated corresponding static page to the FileName field inside the database

$sql = "Update news set Filename= ' $filename _path ' where id= $id";

$res = mysql_query ($sql);

$num = Mysql_affected_rows ($conn);

if (! $num) {

Die ("failure");

}

Echo ' Add success ';

echo "<a href= ' index.php ' > Back home </a>";

}

?>

(2) Complete the background of the homepage.

(3) Create a new makehtml.php page to complete, the foreground home generation.

<?php

$conn = mysql_connect ("localhost", ' root ', ' root ');

mysql_query ("Use PHP");

mysql_query ("Set names UTF8");

$sql = "Select Id,title,filename from News";

$list =array ();

$res = mysql_query ($sql, $conn);

while ($row =mysql_fetch_assoc ($res)) {

$list []= $row;

}

Ob_start ();

?>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<title> New Page </title>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<meta name= "description" content= ""/>

<meta name= "keywords" content= ""/>

<script type= "Text/javascript" >

</script>

<style type= "Text/css" >

</style>

<body>

<table width= "border=" 1 ">

<tr><td> News Headlines </td><td> News details </td><td> Actions </td></tr>

<?php foreach ($list as $v) {?>

<tr><td><?php echo $v [' title ']?></td><td><a href= ' <?php echo $v [' filename ']? > > Press Details </a></td><td> actions </td></tr>

<?php}?>

</table>

</body>

<?php

$str = Ob_get_contents ();

File_put_contents (".. /index.html ", $STR);

Ob_clean ();

echo "Home has already generated <a href= '. /index.html ' > Back to front page </a> ";

?>

(4) test results:

Seven, true static advantages and disadvantages of the description:

Advantages: 1. 2 faster. 3 higher security. Facilitate SEO

Disadvantage: It occupies disk space. If it is too large, it has an effect on the disk response speed.

Under what circumstances, it is recommended that you do not use true static

    1. Page data update frequently, it is best not to use real static (such as stocks, funds, such as real-time quoting system)
    2. Generate huge volumes of pages (such as large forum BBS, CSDN)
    3. Once the page is queried, the page will not be queried again at a later time.
    4. Do not want to be crawled by the search engine page.
    5. A page with a small amount of access.
Pseudo-static, using regular matching to complete pseudo-static.

The way of implementation: using regular matching, through the Apache rewrite mechanism.

Basic diagram:

Specific steps: Copy the morning case dome changed to DEMO3.

(1) Open index.php page to change the link of news details to <a href= ' newsinfo.php/news_id88.html ' > News details </a>

(2) Modify the newsinfo.php page.

<?php

$path _info = $_server[' path_info '];//get to address: news_id1.html

$patt = '/news_id (\d{1,3}) \.html$/';

Preg_match ($patt, $path _info, $a);//$a used to store matching content.

$id = $a [1];

$conn = mysql_connect ("localhost", ' root ', ' root ');

mysql_query ("Use PHP");

mysql_query ("Set names UTF8");

$sql = "Select Title,content from news where id = $id";

$res = mysql_query ($sql, $conn);

$row =mysql_fetch_assoc ($res);

?>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<title> New Page </title>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<meta name= "description" content= ""/>

<meta name= "keywords" content= ""/>

<script type= "Text/javascript" >

</script>

<style type= "Text/css" >

</style>

<body>

<div><?php echo $row [' content ']?></div>

</body>

Nine, using rewrite mechanism, complete pseudo-static. 1. Principle of use of rewrite mechanism

2, the rewrite mechanism of the specific configuration:

(1) Open the APAHCE main configuration file (httpd.conf) and turn on the rewrite module.

(2) In order to test, create a new virtual host, using the morning created.

(3) Starter Case: request abc.html converted to request 123.php, set up the two files under the virtual host directory.

Specific configuration content:

3, the virtual host inside the common configuration.

(1) #配置是否显示文件目录: [Indexes|none]

Options indexes FollowSymLinks//configuration displays the file directory if no welcome page is configured.

If configured as options none

(2) Configure error jump page. The main prevention of user requests for the page does not exist, if the user access to the page does not exist, then give a friendly hint.

(3) Configure the Welcome page, that is, the default execution page when accessing the domain name.

#配置网站的欢迎页面:

DirectoryIndex abc.html

4. Completed a case:

such as the requested URL

Http://www.abc.com/news-music-id100.html

Convert to request: news.php?type=music&id=100 page, or news.php?type=sport&id=100

Specific configuration:

Effect:

5, you can create a new. htaccess file, the specific content can be configured in the file.

Note: To make the file valid, you must set it in the virtual host. AllowOverride All

(1) Create a new. htaccess file,

You can create a. htaccess file by using the editor to save as.

You can complete some configuration options inside.

(2) Define rewrite rules in this file

6, through the use of rewrite mechanism to complete an anti-theft chain case.

Main principle: Using Referer head information,

Specific configuration:

Gz_ Cache Technology 2

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.