PHP data caching technology_PHP tutorial

WBOY
Release: 2016-07-21 15:56:50
Original
1111 people have browsed it

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>

Applicable situations:
1. The data is relatively stable, mainly for reading operations.
2. File operations are faster than database operations.
3. Complex data access, large data volume access, intensive data access, the system database load is extremely heavy.
4.Web/DB separation structure or multi-Web single DB structure.

Unconfirmed issues:
1. Will reading and writing files during concurrent access cause locking problems.
2. How is the performance when there are too many data files involved?
Expansion ideas:
1. Generate JavaScript data definition code and call it on the client.
2. Haven’t thought of it yet...

We hope to discuss it together.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317934.htmlTechArticleData caching is a commonly used performance optimization method in web development. There are currently two main forms of file caching or database caching. Database caching is not impossible,...
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!