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>
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>
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.