Rumah > Artikel > pembangunan bahagian belakang > PHP数据缓存技术_PHP教程
数据缓存是web开发中常用的一种性能优化方法。目前主要文件缓存或者数据库缓存两种形式,数据库缓存数据库不是什么不可能的事情,的确也是很好很重要的。我认为传统数据库主要是从业务层、模块设计等方面来考虑的,而缓存数据库主要是从实现层来设计的,主要是为了缓存常用的多表查询之类的。这里主要将的是文件缓存,网上很多资料了,这里我转载了一些原理资料。
Cache是“以空间换时间”策略的典型应用模式,是提高系统性能的一种重要方法。缓存的使用在大访问量的情况下能够极大的减少对数据库操作的次数,明显降低系统负荷提高系统性能。相比页面的缓存,结果集是一种“原始数据”不包含格式信息,数据量相对较小,而且可以再进行格式化,所以显得相当灵活。由于PHP是“一边编译一边执行”的脚本语言,某种程度上也提供了一种相当方便的结果集缓存使用方法——通过动态include相应的数据定义代码段的方式使用缓存。如果在“RamDisk”上建缓存的话,效率应该还可以得到进一步的提升。以下是一小段示例代码,供参考。
<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 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">";);\r\n"</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">"?>\r\n"</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></span>