Maison >développement back-end >tutoriel php >实例:实现动态网页的静态缓存_PHP教程
源动态网页
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>实例:实现动态网页的静态缓存</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"><</span><span class="tag-name">h3</span><span class="tag">></span><span>实例:实现动态网页的静态缓存</span><span class="tag"></</span><span class="tag-name">h3</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">p</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"><</span><span class="tag-name">chtml</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">for</span><span> </span><span class="attribute">end</span><span>=</span><span class="attribute-value">0</span><span> </span><span class="attribute">start</span><span>=</span><span class="attribute-value">20</span><span class="tag">></span><span> </span></span></li><li><span> <span class="tag"><</span><span class="tag-name">b</span><span class="tag">></span><span>@{int:@{for:getSuffix}+1}-</span><span class="tag"></</span><span class="tag-name">b</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">forendforend</span><span class="attribute">forendforend</span><span>=@{for:getSuffix}</span><span class="tag">></span><span> </span></span></li><li><span> <span class="tag"><</span><span class="tag-name">fontcolorfontcolor</span><span class="attribute">fontcolorfontcolor</span><span>=</span><span class="attribute-value">red</span><span class="tag">></span><span>@{int:@{for:getSuffix}+1}-</span><span class="tag"></</span><span class="tag-name">font</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"></</span><span class="tag-name">for</span><span class="tag">></span><span> </span></span></li><li><span> <span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">for</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"></</span><span class="tag-name">chtml</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">p</span><span class="tag">></span><span> </span></span></li></ol>
(例子文件:_samples/file_page.html)
读缓存的静态文件信息
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">file</span><span> </span><span class="attribute">act</span><span>=</span><span class="attribute-value">info</span><span class="tag">></span><span>WEB-INF/cache/page_test.html</span><span class="tag"></</span><span class="tag-name">file</span><span class="tag">></span><span> </span></span></li></ol>
判断静态文件是否存在
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">if</span><span> </span><span class="attribute">x</span><span>="@{file:exists}"</span><span class="attribute-value">else</span><span>=0</span><span class="tag">></span><span> </span></span></li><li><span> <span class="tag"><</span><span class="tag-name">call</span><span class="tag">></span><span>page</span><span class="tag"></</span><span class="tag-name">call</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">callxcallx</span><span class="attribute">callxcallx</span><span>=</span><span class="attribute-value">true</span><span class="tag">></span><span>cache</span><span class="tag"></</span><span class="tag-name">call</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"></</span><span class="tag-name">if</span><span class="tag">></span><span> </span></span></li></ol>
存在就调用cache,不存在则调用page
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">call</span><span class="tag">></span><span>page</span><span class="tag"></</span><span class="tag-name">call</span><span class="tag">></span><span> </span></span></li><li><span> <span class="tag"><</span><span class="tag-name">call</span><span> </span><span class="attribute">x</span><span>=</span><span class="attribute-value">true</span><span class="tag">></span><span>cache</span><span class="tag"></</span><span class="tag-name">call</span><span class="tag">></span><span> </span></span></li></ol>
可调用标签cache,读缓存文件
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">chtml</span><span> </span><span class="attribute">onlycallid</span><span>=</span><span class="attribute-value">cache</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"><</span><span class="tag-name">file</span><span> </span><span class="attribute">act</span><span>=</span><span class="attribute-value">read</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">str</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">page</span><span class="tag">></span><span>WEB-INF/cache/page_test.html</span><span class="tag"></</span><span class="tag-name">file</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">chtml</span><span class="tag">></span><span> </span></span></li></ol>
可调用标签page
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">chtml</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">page</span><span> </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"@{sys:curPath}file_page.html"</span><span> </span><span class="attribute">save</span><span>=</span><span class="attribute-value">"WEB-INF/cache/page_test.html"</span><span> onlycall </span><span class="tag">/></span><span> </span></span></li></ol>
引入动态网页文件@{sys:curPath}file_page.html,并保存为WEB-INF/cache/page_test.html文件
显示缓存内容
@{file:page}
完整代码
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"><</span><span class="tag-name">chtml</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">file</span><span> </span><span class="attribute">act</span><span>=</span><span class="attribute-value">info</span><span class="tag">></span><span>WEB-INF/cache/page_test.html</span><span class="tag"></</span><span class="tag-name">file</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"><</span><span class="tag-name">if</span><span> </span><span class="attribute">x</span><span>=</span><span class="attribute-value">"@{file:exists}"</span><span> </span><span class="attribute">else</span><span>=</span><span class="attribute-value">0</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">call</span><span class="tag">></span><span>page</span><span class="tag"></</span><span class="tag-name">call</span><span class="tag">></span><span> </span></span></li><li><span> <span class="tag"><</span><span class="tag-name">call</span><span> </span><span class="attribute">x</span><span>=</span><span class="attribute-value">true</span><span class="tag">></span><span>cache</span><span class="tag"></</span><span class="tag-name">call</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">if</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"></</span><span class="tag-name">chtml</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">chtml</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">page</span><span> </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"@{sys:curPath}file_page.html"</span><span> </span><span class="attribute">save</span><span>=</span><span class="attribute-value">"WEB-INF/cache/page_test.html"</span><span> onlycall </span><span class="tag">/></span><span> </span></span></li><li><span><span class="tag"><</span><span class="tag-name">chtml</span><span> onlycall </span><span class="attribute">id</span><span>=</span><span class="attribute-value">cache</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">file</span><span> </span><span class="attribute">act</span><span>=</span><span class="attribute-value">read</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">str</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">page</span><span class="tag">></span><span>WEB-INF/cache/page_test.html</span><span class="tag"></</span><span class="tag-name">file</span><span class="tag">></span><span> </span></span></li><li><span><span class="tag"></</span><span class="tag-name">chtml</span><span class="tag">></span><span>@{file:page} </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li></ol>
效果
(例子文件:_samples/file_cache.html)
抓紧体验体验吧!