Home > Article > Backend Development > PHP data caching technology_PHP tutorial
Data caching is a commonly used performance optimization method in web development. At present, there are two main forms of file caching or database caching. Database caching is not impossible, and it is indeed very good and important. I think traditional databases are mainly considered from the business layer, module design, etc., while cache databases are mainly designed from the implementation layer, mainly to cache commonly used multi-table queries. The main thing here is file caching. There is a lot of information on the Internet. Here I have reproduced some principle information.
Cache is a typical application mode of the "space for time" strategy and an important method to improve system performance. The use of cache can greatly reduce the number of database operations in the case of large access volumes, significantly reduce system load and improve system performance. Compared with page caching, the result set is a kind of "raw data" that does not contain format information. The amount of data is relatively small and can be formatted again, so it is quite flexible. Since PHP is a scripting language that "compiles and executes at the same time", it also provides a very convenient way to use result set caching to a certain extent - using the cache by dynamically including the corresponding data definition code segment. If a cache is built on "RamDisk", the efficiency should be further improved. Below is a small sample code for reference.
<span style="COLOR: #000000"><br><span style="COLOR: #0000bb"><? <BR></SPAN><SPAN style="COLOR: #ff8000">// load data with cache <br><br></SPAN><SPAN style="COLOR: #007700">function </SPAN><SPAN style="COLOR: #0000bb">load_data</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$id</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">$cache_lifetime</SPAN><SPAN style="COLOR: #007700">) { <br><br></SPAN><SPAN style="COLOR: #ff8000">// the return data <br><br></SPAN><SPAN style="COLOR: #0000bb">$data </SPAN><SPAN style="COLOR: #007700">= array(); <br><br></SPAN><SPAN style="COLOR: #ff8000">// make cache filename <br><br></SPAN><SPAN style="COLOR: #0000bb">$cache_filename </SPAN><SPAN style="COLOR: #007700">= </SPAN><SPAN style="COLOR: #0000bb">‘cache_‘</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">$id</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">‘</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">php‘</SPAN><SPAN style="COLOR: #007700">; <br><br></SPAN><SPAN style="COLOR: #ff8000">// check cache file‘s last modify time <br><br></SPAN><SPAN style="COLOR: #0000bb">$cache_filetime </SPAN><SPAN style="COLOR: #007700">= </SPAN><SPAN style="COLOR: #0000bb">filemtime</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$cache_filename</SPAN><SPAN style="COLOR: #007700">); <br><br>if (</SPAN><SPAN style="COLOR: #0000bb">time</SPAN><SPAN style="COLOR: #007700">() - </SPAN><SPAN style="COLOR: #0000bb">$cache_filetime </SPAN><SPAN style="COLOR: #007700"><= </SPAN><SPAN style="COLOR: #0000bb">$cache_lifetime</SPAN><SPAN style="COLOR: #007700">) { <br><br></SPAN><SPAN style="COLOR: #ff8000">//** the cache is not expire <br><br></SPAN><SPAN style="COLOR: #007700">include(</SPAN><SPAN style="COLOR: #0000bb">$cache_filename</SPAN><SPAN style="COLOR: #007700">); <br><br>} else { <br><br></SPAN><SPAN style="COLOR: #ff8000">//** the cache is expired <br><br>// load data from database <br><br>// ... <br><br></SPAN><SPAN style="COLOR: #007700">while (</SPAN><SPAN style="COLOR: #0000bb">$dbo</SPAN><SPAN style="COLOR: #007700">-></span><span style="COLOR: #0000bb">nextRecord</span><span style="COLOR: #007700">()) { <br><br></span><span style="COLOR: #ff8000">// $data[] = ... <br><br></span><span style="COLOR: #007700">} <br><br></span><span style="COLOR: #ff8000">// format the data as a php file <br><br></span><span style="COLOR: #0000bb">$data_cache </span><span style="COLOR: #007700">= </span><span style="COLOR: #dd0000">" <br><br>while (list($key, $val) = each($data)) { <br><br>$data_cache .= "</span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">[</span><span style="COLOR: #0000bb">‘$key‘</span><span style="COLOR: #007700">]=array(</span><span style="COLOR: #0000bb">‘</span><span style="COLOR: #dd0000">"; <br><br>$data_cache .= "</span><span style="COLOR: #0000bb">‘NAME‘</span><span style="COLOR: #007700">=></span><span style="COLOR: #dd0000">""</span><span style="COLOR: #007700">.</span><span style="COLOR: #0000bb">qoute</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$val</span><span style="COLOR: #007700">[</span><span style="COLOR: #0000bb">‘NAME‘</span><span style="COLOR: #007700">]).</span><span style="COLOR: #dd0000">""," <br><br></span><span style="COLOR: #0000bb">$data_cache </span><span style="COLOR: #007700">.= </span><span style="COLOR: #dd0000">"‘VALUE‘=>""</span><span style="COLOR: #007700">.</span><span style="COLOR: #0000bb">qoute</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$val</span><span style="COLOR: #007700">[</span><span style="COLOR: #0000bb">‘VALUE‘</span><span style="COLOR: #007700">]).</span><span style="COLOR: #dd0000">""" <br><br></span><span style="COLOR: #0000bb">$data_cache </span><span style="COLOR: #007700">.= </span><span style="COLOR: #dd0000">";);rn"</span><span style="COLOR: #007700">; <br><br>}<br><br></span><span style="COLOR: #0000bb">$data_cache </span><span style="COLOR: #007700">= </span><span style="COLOR: #dd0000">"?>rn"</span><span style="COLOR: #007700">; <br><br></span><span style="COLOR: #ff8000">// save the data to the cache file <br><br></span><span style="COLOR: #007700">if (</span><span style="COLOR: #0000bb">$fd </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">fopen</span> <span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$cache_filename</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb">'w</span><span style="COLOR: #007700">+</span><span style="COLOR: #0000bb">'</span><span style="COLOR: #007700">)) { <br><br></span><span style="COLOR: #0000bb">fputs</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$fd</span><span style="COLOR: #007700">,</span><span style="COLOR: #0000bb">$data_cache</span><span style="COLOR: #007700">) ; <br><br></span><span style="COLOR: #0000bb">fclose</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$fd</span><span style="COLOR: #007700">); <br><br>} <br><br> } <br><br>return </span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">; <br><br>} <br><br></span><span style="COLOR: #0000bb">?></span> <br><br></span>