php 缓存动态内容
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 缓存动态内容相关的知识,希望对你有一定的参考价值。
<span style="color: #808080; font-style: italic;">// Start output buffering, this will</span>
<span style="color: #808080; font-style: italic;">// catch all content so that we can </span>
<span style="color: #808080; font-style: italic;">// do some calculations</span>
<a href="http://www.php.net/ob_start"><span style="color: #000066;">ob_start</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Some example HTML</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<html>'</span>;
<span style="color: #808080; font-style: italic;">// put your content in here:</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<h1>Example content</h1>'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<ul>'</span>;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$x</span>=<span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$x</span> < <span style="color: #cc66cc;">10</span>; <span style="color: #0000ff;">$x</span>++<span style="color: #66cc66;">)</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"<li>List item $x</li>"</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'</ul>'</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'</html>'</span>;
<span style="color: #808080; font-style: italic;">// or include() something here</span>
<span style="color: #808080; font-style: italic;">// Now save all the content from above into</span>
<span style="color: #808080; font-style: italic;">// a variable</span>
<span style="color: #0000ff;">$PageContent</span> = <a href="http://www.php.net/ob_get_contents"><span style="color: #000066;">ob_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// And clear the buffer, so the</span>
<span style="color: #808080; font-style: italic;">// contents will not be submitted to </span>
<span style="color: #808080; font-style: italic;">// the client (we do that later manually)</span>
<a href="http://www.php.net/ob_end_clean"><span style="color: #000066;">ob_end_clean</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Generate unique Hash-ID by using MD5</span>
<span style="color: #0000ff;">$HashID</span> = <a href="http://www.php.net/md5"><span style="color: #000066;">md5</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$PageContent</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Specify the time when the page has</span>
<span style="color: #808080; font-style: italic;">// been changed. For example this date</span>
<span style="color: #808080; font-style: italic;">// can come from the database or any</span>
<span style="color: #808080; font-style: italic;">// file. Here we define a fixed date value:</span>
<span style="color: #0000ff;">$LastChangeTime</span> = <span style="color: #cc66cc;">1144055759</span>;
<span style="color: #808080; font-style: italic;">// Define the proxy or cache expire time </span>
<span style="color: #0000ff;">$ExpireTime</span> = <span style="color: #cc66cc;">3600</span>; <span style="color: #808080; font-style: italic;">// seconds (= one hour)</span>
<span style="color: #808080; font-style: italic;">// Get request headers:</span>
<span style="color: #0000ff;">$headers</span> = <a href="http://www.php.net/apache_request_headers"><span style="color: #000066;">apache_request_headers</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// you could also use getallheaders() or $_SERVER</span>
<span style="color: #808080; font-style: italic;">// or HTTP_SERVER_VARS </span>
<span style="color: #808080; font-style: italic;">// Add the content type of your page</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-Type: text/html'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Content language</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-language: en'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Set cache/proxy informations:</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Cache-Control: max-age='</span> . <span style="color: #0000ff;">$ExpireTime</span><span style="color: #66cc66;">)</span>; <span style="color: #808080; font-style: italic;">// must-revalidate</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Expires: '</span>.<a href="http://www.php.net/gmdate"><span style="color: #000066;">gmdate</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'D, d M Y H:i:s'</span>, <a href="http://www.php.net/time"><span style="color: #000066;">time</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>+<span style="color: #0000ff;">$ExpireTime</span><span style="color: #66cc66;">)</span>.<span style="color: #ff0000;">' GMT'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Set last modified (this helps search engines </span>
<span style="color: #808080; font-style: italic;">// and other web tools to determine if a page has</span>
<span style="color: #808080; font-style: italic;">// been updated)</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Last-Modified: '</span>.<a href="http://www.php.net/gmdate"><span style="color: #000066;">gmdate</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'D, d M Y H:i:s'</span>, <span style="color: #0000ff;">$LastChangeTime</span><span style="color: #66cc66;">)</span>.<span style="color: #ff0000;">' GMT'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Send a "ETag" which represents the content</span>
<span style="color: #808080; font-style: italic;">// (this helps browsers to determine if the page</span>
<span style="color: #808080; font-style: italic;">// has been changed or if it can be loaded from</span>
<span style="color: #808080; font-style: italic;">// the cache - this will speed up the page loading)</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'ETag: '</span> . <span style="color: #0000ff;">$HashID</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// The browser "asks" us if the requested page has</span>
<span style="color: #808080; font-style: italic;">// been changed and sends the last modified date he</span>
<span style="color: #808080; font-style: italic;">// has in it's internal cache. So now we can check</span>
<span style="color: #808080; font-style: italic;">// if the submitted time equals our internal time value.</span>
<span style="color: #808080; font-style: italic;">// If yes then the page did not get updated</span>
<span style="color: #0000ff;">$PageWasUpdated</span> = !<span style="color: #66cc66;">(</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'If-Modified-Since'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> and
<a href="http://www.php.net/strtotime"><span style="color: #000066;">strtotime</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'If-Modified-Since'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> == <span style="color: #0000ff;">$LastChangeTime</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// The second possibility is that the browser sends us</span>
<span style="color: #808080; font-style: italic;">// the last Hash-ID he has. If he does we can determine</span>
<span style="color: #808080; font-style: italic;">// if he has the latest version by comparing both IDs. </span>
<span style="color: #0000ff;">$DoIDsMatch</span> = <span style="color: #66cc66;">(</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'If-None-Match'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span> and
<a href="http://www.php.net/ereg"><span style="color: #000066;">ereg</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$HashID</span>, <span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'If-None-Match'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Does one of the two ways apply?</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$PageWasUpdated</span> or <span style="color: #0000ff;">$DoIDsMatch</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// Okay, the browser already has the</span>
<span style="color: #808080; font-style: italic;">// latest version of our page in his</span>
<span style="color: #808080; font-style: italic;">// cache. So just tell him that</span>
<span style="color: #808080; font-style: italic;">// the page was not modified and DON'T</span>
<span style="color: #808080; font-style: italic;">// send the content -> this saves bandwith and</span>
<span style="color: #808080; font-style: italic;">// speeds up the loading for the visitor</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'HTTP/1.1 304 Not Modified'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// That's all, now close the connection:</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Connection: close'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// The magical part: </span>
<span style="color: #808080; font-style: italic;">// No content here ;-) </span>
<span style="color: #808080; font-style: italic;">// Just the headers</span>
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// Okay, the browser does not have the</span>
<span style="color: #808080; font-style: italic;">// latest version or does not have any</span>
<span style="color: #808080; font-style: italic;">// version cached. So we have to send him</span>
<span style="color: #808080; font-style: italic;">// the full page.</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'HTTP/1.1 200 OK'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Tell the browser which size the content</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-Length: '</span> . <a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$PageContent</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Send the full content</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$PageContent</span>;
<span style="color: #66cc66;">}</span>
以上是关于php 缓存动态内容的主要内容,如果未能解决你的问题,请参考以下文章