Bookmark and Share

Lee's 程序人生

HTML CSS Javascript XML AJAX ATLAS C# C++ 数据结构 软件工程 设计模式 asp.net Java 数字图象处理 Sql 数据库
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

php提高网站性能之PHP数据缓存技术详解

Posted on 2008-06-13 10:15  analyzer  阅读(1080)  评论(1)    收藏  举报
数据缓存是web开发中常用的一种性能优化方法。目前主要文件缓存或者数据库缓存两种形式,数据库缓存数据库不是什么不可能的事情,的确也是很好很重要的。我认为传统数据库主要是从业务层、模块设计等方面来考虑的,而缓存数据库主要是从实现层来设计的,主要是为了缓存常用的多表查询之类的。这里主要将的是文件缓存,网上很多资料了,这里我转载了一些原理资料。
    Cache是“以空间换时间”策略的典型应用模式,是提高系统性能的一种重要方法。缓存的使用在大访问量的情况下能够极大的减少对数据库操作的次数,明显降低系统负荷提高系统性能。相比页面的缓存,结果集是一种“原始数据”不包含格式信息,数据量相对较小,而且可以再进行格式化,所以显得相当灵活。由于PHP是“一边编译一边执行”的脚本语言,某种程度上也提供了一种相当方便的结果集缓存使用方法——通过动态include相应的数据定义代码段的方式使用缓存。如果在“RamDisk”上建缓存的话,效率应该还可以得到进一步的提升。以下是一小段示例代码,供参考。
 1function load_data($id,$cache_lifetime) {     
 2   
 3// the return data     
 4   
 5$data = array();     
 6   
 7// make cache filename     
 8   
 9$cache_filename = ‘cache_‘.$id..php‘;     
10   
11// check cache file‘s last modify time     
12   
13$cache_filetime = filemtime($cache_filename);     
14   
15if (time() - $cache_filetime <= $cache_lifetime) {     
16   
17//** the cache is not expire     
18   
19include($cache_filename);     
20   
21else {     
22   
23//** the cache is expired     
24   
25// load data from database     
26   
27//      
28   
29while ($dbo->nextRecord()) {     
30   
31// $data[] =      
32   
33}     
34   
35// format the data as a php file     
36   
37$data_cache = "    
38  
39while (list($key$val) = each($data)) {    
40  
41$data_cache .= "$data[‘$key‘]=array(‘";    
42  
43$data_cache .= "‘NAME‘=>"".qoute($val[‘NAME‘])."\","     
44   
45$data_cache .= "‘VALUE‘=>\"".qoute($val[‘VALUE‘])."\""     
46   
47$data_cache .= ";);";     
48   
49}     
50   
51$data_cache = "?>";     
52   
53// save the data to the cache file     
54   
55if ($fd = fopen($cache_filename,‘w+‘)) {     
56   
57fputs($fd,$data_cache);     
58   
59fclose($fd);     
60   
61}     
62   
63}     
64   
65return $data;     
66   
67}     
68   
69?>    

 适用情况:
    1.数据相对比较稳定,主要是读取操作。
    2.文件操作要比数据库操作快。
    3.复杂数据访问,大数据量访问,密集数据访问,系统数据库负载极重。
    4.Web/DB分离结构或者多Web单DB结构。

////////////补充

 


 1<?php     
 2function cache_isvalid($cacheid,$expire=300) {     
 3@clearstatcache();     
 4if (!@file_exists($cacheid)) return false;     
 5if (!($mtime=@filemtime($cacheid))) return false;     
 6$nowtime=mktime();     
 7if (($mtime+$expire)<$nowtime) {     
 8return false;     
 9}else{     
10return true;     
11}     
12}     
13   
14function cache_write($cacheid,$cachecontent) {     
15$retry=100;     
16for ($i=0;$i<$retry;$i++) {     
17$ft=@fopen($cacheid,"wb");     
18if ($ft!=falsebreak;     
19if ($i==($retry-1)) return false;     
20}     
21@flock($ft,LOCK_UN);     
22@flock($ft,LOCK_EX|LOCK_NB);     
23for ($i=0;$i<$retry;$i++) {     
24$tmp=@fwrite($ft,$cachecontent);     
25if ($tmp!=falsebreak;     
26if ($i==($retry-1)) return false;     
27}     
28@flock($ft,LOCK_UN);     
29@fclose($ft);     
30@chmod($cacheid,0777);     
31return true;     
32}     
33   
34function cache_fetch($cacheid) {     
35$retry=100;     
36for ($i=0;$i<$retry;$i++) {     
37$ft=@fopen($cacheid,"rb");     
38if ($ft!=falsebreak;     
39if ($i==($retry-1)) return false;     
40}     
41$cachecontent='';     
42while (!@feof($ft)) {     
43$cachecontent.=@fread($ft,4096);     
44}     
45@fclose($ft);     
46return $cachecontent;     
47}     
48   
49function cache_clear_expired($cachedirname,$expire=300) {     
50$cachedir=@opendir($cachedirname);     
51while (false!==($userfile=@readdir($cachedir))) {     
52if ($userfile!="." and $userfile!=".." and substr($userfile,-4,4)=='.htm') {     
53$cacheid=$cachedirname.'/'.$userfile;     
54if (!cache_isvalid($cacheid,$expire)) @unlink($cacheid);     
55}     
56}     
57@closedir($cachedir);     
58}     
59   
60?>