Rumah > Artikel > pembangunan bahagian belakang > 2009nba总决赛数据 PHP数据缓存技术
数据缓存是web开发中常用的一种性能优化方法。目前主要文件缓存或者数据库缓存两种形式,数据库缓存数据库不是什么不可能的事情,的确也是很好很重要的。我认为传统数据库主要是从业务层、模块设计等方面来考虑的,而缓存数据库主要是从实现层来设计的,主要是为了缓存常用的多表查询之类的。这里主要将的是文件缓存,网上很多资料了,这里我转载了一些原理资料。
Cache是“以空间换时间”策略的典型应用模式,是提高系统性能的一种重要方法。缓存的使用在大访问量的情况下能够极大的减少对数据库操作的次数,明显降低系统负荷提高系统性能。相比页面的缓存,结果集是一种“原始数据”不包含格式信息,数据量相对较小,而且可以再进行格式化,所以显得相当灵活。由于PHP是“一边编译一边执行”的脚本语言,某种程度上也提供了一种相当方便的结果集缓存使用方法——通过动态include相应的数据定义代码段的方式使用缓存。如果在“RamDisk”上建缓存的话,效率应该还可以得到进一步的提升。以下是一小段示例代码,供参考。
<span><br><span> <br></span><span>// load data with cache <br></span><span>function </span><span>load_data</span><span>(</span><span>$id</span><span>,</span><span>$cache_lifetime</span><span>) { <br></span><span>// the return data <br></span><span>$data </span><span>= array(); <br></span><span>// make cache filename <br></span><span>$cache_filename </span><span>= </span><span>‘cache_‘</span><span>.</span><span>$id</span><span>.</span><span>‘</span><span>.</span><span>php‘</span><span>; <br></span><span>// check cache file‘s last modify time <br></span><span>$cache_filetime </span><span>= </span><span>filemtime</span><span>(</span><span>$cache_filename</span><span>); <br>if (</span><span>time</span><span>() - </span><span>$cache_filetime </span><span><span>$cache_lifetime</span><span>) { <br></span><span>//** the cache is not expire <br></span><span>include(</span><span>$cache_filename</span><span>); <br>} else { <br></span><span>//** the cache is expired <br>// load data from database <br>// ... <br></span><span>while (</span><span>$dbo</span><span>-></span><span>nextRecord</span><span>()) { <br></span><span>// $data[] = ... <br></span><span>} <br></span><span>// format the data as a php file <br></span><span>$data_cache </span><span>= </span><span>" <br>while (list($key, $val) = each($data)) { <br>$data_cache .= "</span><span>$data</span><span>[</span><span>‘$key‘</span><span>]=array(</span><span>‘</span><span>"; <br>$data_cache .= "</span><span>‘NAME‘</span><span>=></span><span>""</span><span>.</span><span>qoute</span><span>(</span><span>$val</span><span>[</span><span>‘NAME‘</span><span>]).</span><span>"\"," <br></span><span>$data_cache </span><span>.= </span><span>"‘VALUE‘=>\""</span><span>.</span><span>qoute</span><span>(</span><span>$val</span><span>[</span><span>‘VALUE‘</span><span>]).</span><span>"\"" <br></span><span>$data_cache </span><span>.= </span><span>";);\r\n"</span><span>; <br>} <br></span><span>$data_cache </span><span>= </span><span>"?>\r\n"</span><span>; <br></span><span>// save the data to the cache file <br></span><span>if (</span><span>$fd </span><span>= </span><span>fopen</span><span>(</span><span>$cache_filename</span><span>,</span><span>‘w</span><span>+</span><span>‘</span><span>)) { <br></span><span>fputs</span><span>(</span><span>$fd</span><span>,</span><span>$data_cache</span><span>); <br></span><span>fclose</span><span>(</span><span>$fd</span><span>); <br>} <br>} <br>return </span><span>$data</span><span>; <br>} <br></span><span>?></span> <br></span></span>
以上就介绍了2009nba总决赛数据 PHP数据缓存技术,包括了2009nba总决赛数据方面的内容,希望对PHP教程有兴趣的朋友有所帮助。