如何提高PHP速度第1/3页_php技巧

PHP中文网
發布: 2016-05-17 09:41:02
原創
917 人瀏覽過

简单的数据缓存技术 

  近来做了一阵子程序性能的优化工作,有个比较有意思的想法,想提出来和大家交流一下。 
  Cache是“以空间换时间”策略的典型应用模式,是提高系统性能的一种重要方法。缓存的使用在大访问量的情况下能够极大的减少对数据库操作的次数,明显降低系统负荷提高系统性能。相比页面的缓存,结果集是一种“原始数据”不包含格式信息,数据量相对较小,而且可以再进行格式化,所以显得相当灵活。由于PHP是“一边编译一边执行”的脚本语言,某种程度上也提供了一种相当方便的结果集缓存使用方法——通过动态include相应的数据定义代码段的方式使用缓存。如果在“RamDisk”上建缓存的话,效率应该还可以得到进一步的提升。以下是一小段示例代码,供参考。 

<?  
// load data with cache  
function load_data($id,$cache_lifetime) {  
// the return data  
$data = array();  
// make cache filename  
$cache_filename = ‘cache_‘.$id.‘.php‘;  
// check cache file‘s last modify time  
$cache_filetime = filemtime($cache_filename);  
if (time() - $cache_filetime <= $cache_lifetime) {  
//** the cache is not expire  
include($cache_filename);  
} else {  
//** the cache is expired  
// load data from database  
// ...  
while ($dbo->nextRecord()) {  
// $data[] = ...  
}  
// format the data as a php file  
$data_cache = "
登入後複製


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!