呵呵~~这种技术在大型网站的应用其实是很广泛的~~只是没有使用统一的产品~~比如我们新华的首页吧`~实际是每天0点使用一个程序(呵呵~~商业机密了~~)将动态的index_d.php解析一次,生成一个静态的index.php,实际里面全是html代码。这样可以大大降低服务器的负载,因为毕竟有很多人只是看看首页的,在首页里如果有太多的与服务器的交互那不就惨啦`~:)
调试欢乐多
回复人: sillyfox(傻狐狸) (2002-1-23 21:04:54) 得0分
还有个问题,如何防止生成静态页时两个用户同时写静态文件或在写文件的同时有用户读生成的静态页??请大家一定要帮忙啊~~~~
很好解决啊
就是在数据库那一层产生一个死锁(update)
同时:另一个人也想写,但是有人正在写,那么你就把这个想写的人一个死锁(update)直到那个人写完了在把这个权限给这个现在被你(update)的人。
请里不要给我发垃圾邮件!!!!!!!!!!!垃圾
<?php
//这是一个简单的缓存例子:
//在本页面中需要随即从几个表中抽取N条记录显示, 因为对数据库操作频繁,
//所以采用缓存方法, 24小时内保持不变, 超过则重新生成.
//本例子还借用了模板的功能来生成页面.
//本文有两种随即产生记录的方法, 分别适应索引Id连续和不连续两种情况,
//仅供参考, 具体情况请具体分析.
//[email protected] //缓存文件名:
$cache_file = 'index.inc'; if (time()-filemtime($cache_file) < 3600*24){
//如果缓存文件的修改时间没超过24小时
include ($cache_file);
exit;
} //home page of learn
require "../lib/template.php";
require "../lib/common.php";
require "../lib/config.php";
//require "common.php";
//require "config.php"; $IllnessCount = 5;
$AssayCount = 5;
$RadiologyCount =5; $IllnessList = &get_rand_field('Illness','Name',$IllnessCount);
$AssayList = &get_rand_field('Assay','Name',$AssayCount);
$RadiologyList = &get_rand_field('Radiology','Name',$RadiologyCount);
function &get_rand_field($table,$field,$n){
//$table is identified by Id
$q = "select count(*) from $table";
$total = mysql_result(mysql_query($q),0,0);
$n = min($total,$n); //如果数据的主索引按Id连续:
$id_list=array();
while (sizeof($id_list)<$n){
mt_srand ((double) microtime() * 1000000);
$rand = mt_rand(0,$total-1);
if (in_array($rand,$id_list))
continue;
$id_list[] = $rand;
}
$q = "select Id,$field from $table where Id in(".implode($id_list,',').")";
$r = mysql_query($q) or error_die("查询出错");
return $r; /* 如果主索引Id非连续: (中间记录可能被删除过)
for ($i=0; $i<$n; $i++){
$id_lists= implode($id_list,',');
mt_srand ((double) microtime() * 1000000);
$rand = mt_rand(0,$total-1);
$q = "select Id,$field from $table ";
if ($id_lists != '')
$q .= "where Id not in($id_lists) ";
$q .= "limit $rand,1";
$r = mysql_query($q) or error_die("查询出错");
$id_list[] = mysql_result($r,0,0);
mysql_data_seek($r,0);
$r_list[] = mysql_fetch_object($r);
$total--;
}
return $r_list;
*/
} //output to html
$t = new Template(".", "keep");
$t->set_file(array("page" => "index.ihtml")); $t->set_var(array(
"EmptyBlock" => " "
)); $t->set_block("page", "Loop_Illness", "a");
foreach($IllnessList as $row) {
$t->set_var(array(
"RandIllness" => html_echo($row->Name),
"IllnessId" => $row->Id,
));
$t->parse("a", "Loop_Illness", true);
}
if (sizeof($IllnessList)==0)
$t->parse("a", "EmptyBlock", true); $t->set_block("page", "Loop_Assay", "loop_assay");
foreach($AssayList as $row) {
$t->set_var(array(
"RandAssay" => html_echo($row->Name),
"AssayId" => $row->Id,
));
$t->parse("loop_assay", "Loop_Assay", true);
}
if (sizeof($AssayList)==0)
$t->parse("loop_assay", "EmptyBlock", true); $t->set_block("page", "Loop_Radiology", "loop_radiology");
foreach($RadiologyList as $row) {
$t->set_var(array(
"RandRadiology" => html_echo($row->Name),
"RadiologyId" => $row->Id,
));
$t->parse("loop_radiology", "Loop_Radiology", true);
}
if (sizeof($RadiologyList)==0)
$t->parse("loop_radiology", "EmptyBlock", true); //输出新生成的页面
$t->pparse("out", array("page")); //保存缓冲页面
$fp = fopen($cache_file,'w');
fputs($fp,$t->subst("out"));
fclose($fp);
?>
此函数告诉 PHP 处理器把所有输出重定向到内部缓冲,调用这个函数后,就不会有输出到浏览器。
程序编的蛮专业的嘛
想法不错
不能用怎么回事情??急啊~~~~救命