PHP true static cache ob system function PHP function return value PHP array PHP date function

WBOY
Release: 2016-07-29 08:53:24
Original
1178 people have browsed it

True static cache: (really generates an html page and calls the html directly in the next operation)
The first time (refresh the web page) to determine whether there is a cache file (can be set for a time)
If it does not exist, the first time is to connect to the database (according to PDO), find the data, output it to the page, and cache (write) it to a local .html file at the same time;
Again (refresh the web page) to determine whether the cache file exists
When a cache file exists, directly search the local cache (file_get_content) and read it out

The following code is written in the ci framework

<code><span>$time</span> = <span>10</span>; <span>//设置缓存页面过期时间</span><span>if</span>(!is_file(<span>"obtest.html"</span>) || time()-filemtime(<span>"obtest.html"</span>)><span>$time</span>){
ob_start();       <span>//打开缓冲区   </span><span>$arr</span>[<span>'res'</span>]=<span>$this</span>->db->get(<span>'user'</span>)->result_array();
<span>$this</span>->load->view(<span>'welcome_message'</span>,<span>$arr</span>);
<span>//print_r($arr);</span><span>$info</span> = ob_get_contents();       <span>//得到缓冲区的内容并且赋值给$info   </span><span>$file</span> = <span>fopen</span>(<span>'obtest.html'</span>, <span>'w'</span>);  <span>//打开文件info.txt   </span><span>fwrite</span>(<span>$file</span>, <span>$info</span>);       <span>//写入信息到info.txt   </span><span>fclose</span>(<span>$file</span>);          <span>//关闭文件info.txt</span>
}<span>else</span>{
    echo <span>"cache:"</span>;
    echo file_get_contents(<span>"obtest.html"</span>);
}
</code>
Copy after login

fopen and filemtime methods are used:

<code><span><span>fopen</span><span>()</span> 函数打开文件或者 URL。<span>fopen</span>(文件名称,文件访问类型)<span>w</span>写入方式打开。

<span>filemtime</span><span>()</span> 函数返回文件内容上次的修改时间,里面跟文件名。</span></code>
Copy after login
').addClass('pre-numbering'). hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the php true static cache ob system functions, including php and function content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!