[Development required] basic cache concepts

Source: Internet
Author: User


Source: php development and learning portal welcome to my personal website

1. Client Cache

<Meta> tag, which is the most basic method for page caching.

1 <Meta http-equiv ="Expires" Content ="Mon, 25 Feb 2014 00:00:00 GMT"/>
2 <Meta http-equiv ="Pragma" Content ="No-Cache"/>

The meta tag sent to the HTML page of the browser tells the browser about the cache time and whether to enable the cache. Programa: No-cache content is not guaranteed, but most browsers follow this convention.

A better way is to use the header function to send HTTP headers.

1 <? PHP
2 Header ('Expires: Mon, 25 Feb 2014 00:00:00 gmt');
3 Header ('Pragma: No-cache');
4 ?>

You can further use the cache-control header of http1.1:

1 <? PHP
2 Header ('Expires: Mon, 25 Feb 2014 00:00:00 gmt');
3 Header ('Cache-control: No-store, no-cache, must-revalidate');
4 Header ('Cache-control: Post-check = 0, pre-check = 0', False );
5 Header ('Pragma: No-cache');
6 ?>


The problem with the above method is that the browser must download the page to read the meta tag. When the browser first requests the page, if there is no tag, it will continue to download the cached copy of the source file. If a cached Proxy Server exists between the browser and the server, the browser may get outdated pages. At the same time, the use of HTTP for Cache control is not completely guaranteed. cache-control and pragam are only added for insurance purposes, and the Expires header may fail due to the date setting error on the client computer.

The last-modified and if-modified-since headers can be used to better control the client cache based on client requests. This behavior is called to execute a conditional GET request.

This method requires that the last-modified header be sent every time you access the PHP file. the browser sends the IF-modified-since header containing the updated time on the next request page, the script checks whether the page content is updated after this time. If there is no update, a 304 status code is returned and then exited.

01 <? PHP
02 ...// Get the filemtime
03 Header ('Last-modified :' .Gmdate('D, D m y h: I: s',$ Lastfilemtime).'Gmt');
04
05 $ Request =Getallheaders();
06 If (Isset ($ Requeset['If-modified-since'])
07 {
08 $ Modifiedsince =Explode(';' $ Request['If-modified-since']);
09 $ Modifiedsince =Strtotime($ Modifiedsince[0]);
10 }
11 Else
12 {
13 $ Modifiedsince = 0;
14 }
15 If ($ Lastmodified <=$ Ifmodifiedsince)
16 {
17 Header ('Http/1.1 304 not modified');
18 Exit();
19 }
20 ....// Output content
21 ?>

2. Server cache-Use PHP output buffer

The built-in caching mechanism of PHP is the output buffer. When Echo or print is used, you can use the output control function (beginning with OB) to store the content in the memory buffer.

First, you can store the requested page in the buffer before output. If an error occurs on the page, you can hide the error without being seen by the visitor.

Second, you can store the content of the output buffer to a file. In the next request, you can determine the modification time and then directly output the file without expiration to improve the website access performance. This is also a very good way to implement dynamic static website cache.

01 <? PHP
02 If (File_exist ('./Cache. page')&&$ Lasttime> =Filemtime('./Cache. page'))
03 {
04 Readfile ('./Cache. page');
05 Exit;
06 }
07
08 Ob_start ();
09 Echo <EOT
10 .......
11 .......
12 EOT;
13
14 $ Buffer = Ob_get_contents ();
15 Ob_end_flush ();
16
17 $ Fp =Fopen('./Cache. page','W');
18 Fwrite ($ Fp,$ Buffer);
19 Fclose ($ Fp);

At the same time, this method can be used to cache some infrequently changed parts of the page to different files, and then assemble the entire page during the request to return the response.


[Development required] basic cache concepts

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.