Discuz!官方免费开源建站系统

 找回密码
 立即注册
搜索

缓存论坛数据,优化四格及首页速度

[复制链接]
元首 发表于 2009-5-5 09:13:50 | 显示全部楼层 |阅读模式
本帖最后由 元首 于 2009-5-5 09:17 编辑
  1. GMT+8, 2009-5-5 09:00, Processed in 0.021464 second(s), 3 queries
复制代码
仅使用3次查询, 即可以完成首页显示.

disucz本身是有缓存系统的, 可缓存的过程被固定了, 没办法新增加缓存内容及方法. 为此, 写了个插件来支持无限的缓存意识, 让部分查询SQL可以稍休息片刻. 这样的作用在于,减轻SQL压力, 更快显示出内容.

安装方法:
首先下载function.rar,将里面的php文件上传在根目录.
下载

接着打开: index.php
  1. $discuz_action = 1;
复制代码
的上一行增加:
  1. include_once('function.php');
复制代码
引入function.php文件,里面包含着两个函数.

接着就可以使用了. 也讲讲原理.比如

$str = cache_read('str',3600); 首先读一下数据   
if(is_array($str)===false){
         //如果读不到数据, 当然就进来了.
         $str = '我是数据';
         cache_write('str',$str);  //再把数据写进去.
}
  echo $str;   // 结果总是一样的.

两个函数的参数有:
cache_read('标识',过期时间,缓存文件名);
cache_write('标识',$str,缓存文件名);

现在我们来优化首页四格.
  1. $allarray = cache_read('all',3600);
  2. if(is_array($allarray)===false){
  3. //----首页五格代码开始 缓存开始
  4. $colorarray = array('', 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'gray');
  5. //新贴
  6. $hack_cut_str = 28; //标题字数
  7. $hack_cut_strauthor = 9;
  8. $new_post_threadlist = array();
  9. $nthread = array();
  10. $query = $db->query("SELECT t.*, f.name FROM {$tablepre}threads t, {$tablepre}forums f WHERE t.fid<>'$fid' AND f.fid=t.fid AND f.fid not in (0) AND t.displayorder not in (-1,-2) ORDER BY t.dateline DESC LIMIT 0, 10");
  11. while($nthread = $db->fetch_array($query)) {
  12.         $nthread['forumname'] = ereg_replace('<[^>]*>','',$nthread['name']);
  13.         $nthread['view_subject'] = cutstr($nthread['subject'],$hack_cut_str);
  14.         $nthread['view_author'] = cutstr($nthread['author'],$hack_cut_strauthor);
  15.         $nthread['date']= gmdate("$dateformat $timeformat", $nthread['dateline'] + $timeoffset * 3600);
  16.         $nthread['lastreplytime']= gmdate("$dateformat $timeformat", $nthread[lastpost] + ($timeoffset * 3600));
  17.         if($nthread['highlight']) {
  18.                 $string = sprintf('%02d', $nthread['highlight']);
  19.                 $stylestr = sprintf('%03b', $string[0]);
  20.                 $nthread['highlight'] = 'style="';
  21.                 $nthread['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
  22.                 $nthread['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
  23.                 $nthread['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
  24.                 $nthread['highlight'] .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';
  25.                 $nthread['highlight'] .= '"';
  26.         } else {
  27.                 $nthread['highlight'] = '';
  28.         }
  29.         $new_post_threadlist[] = $nthread;
  30. }
  31. //新回复
  32. $hack_cut_str = 28; //标题字数
  33. $hack_cut_strauthor = 9;
  34. $new_reply_threadlist = array();
  35. $rthread = array();
  36. $query = $db->query("SELECT t.*, f.name FROM {$tablepre}threads t, {$tablepre}forums f WHERE t.fid<>'$fid' AND f.fid=t.fid  AND t.closed NOT LIKE 'moved|%' AND t.replies !=0 AND f.fid not in (0) AND t.displayorder not in (-1,-2) ORDER BY t.lastpost DESC LIMIT 0, 10");
  37. while($rthread = $db->fetch_array($query)) {
  38.         $rthread['forumname'] = ereg_replace('<[^>]*>','',$rthread['name']);
  39.         $rthread['view_subject'] = cutstr($rthread['subject'],$hack_cut_str);
  40.         $rthread['view_lastposter'] = cutstr($rthread['lastposter'],$hack_cut_strauthor);
  41.                 $rthread['date']= gmdate("$dateformat $timeformat", $rthread['dateline'] + $timeoffset * 3600);
  42.         $rthread['lastreplytime']= gmdate("$dateformat $timeformat", $rthread[lastpost] + ($timeoffset * 3600));
  43.         if($rthread['highlight']) {
  44.                 $string = sprintf('%02d', $rthread['highlight']);
  45.                 $stylestr = sprintf('%03b', $string[0]);
  46.                 $rthread['highlight'] = 'style="';
  47.                 $rthread['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
  48.                 $rthread['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
  49.                 $rthread['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
  50.                 $rthread['highlight'] .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';
  51.                 $rthread['highlight'] .= '"';
  52.         } else {
  53.                 $rthread['highlight'] = '';
  54.         }
  55.         $new_reply_threadlist[] = $rthread;
  56. }

  57. //本月热帖
  58. $hack_cut_str = 30; //标题字数
  59. $hack_cut_strauthor = 9;
  60. $new_hot_threadlist = array();
  61. $mthread = array();
  62. $ctime=$timestamp-3600*24*30;//最后30是天数为本月  
  63. $query = $db->query("SELECT t.*, f.name FROM {$tablepre}threads t, {$tablepre}forums f WHERE t.fid<>'$fid' AND f.fid=t.fid  AND t.closed NOT LIKE 'moved|%' AND t.replies !=0 AND t.dateline>$ctime AND f.fid not in (0) AND t.displayorder not in (-1,-2) ORDER BY t.replies DESC LIMIT 0, 10");
  64. while($mthread = $db->fetch_array($query)) {
  65.         $mthread['forumname'] = ereg_replace('<[^>]*>','',$mthread['name']);
  66.         $mthread['view_subject'] = cutstr($mthread['subject'],$hack_cut_str);
  67.         $mthread['view_lastposter'] = cutstr($mthread['lastposter'],$hack_cut_strauthor);
  68.                 $mthread['date']= gmdate("$dateformat $timeformat", $mthread['dateline'] + $timeoffset * 3600);
  69.         $mthread['lastreplytime']= gmdate("$dateformat $timeformat", $mthread[lastpost] + ($timeoffset * 3600));
  70.         if($mthread['highlight']) {
  71.                 $string = sprintf('%02d', $mthread['highlight']);
  72.                 $stylestr = sprintf('%03b', $string[0]);
  73.                 $mthread['highlight'] = 'style="';
  74.                 $mthread['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
  75.                 $mthread['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
  76.                 $mthread['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
  77.                 $mthread['highlight'] .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';
  78.                 $mthread['highlight'] .= '"';
  79.         } else {
  80.                 $mthread['highlight'] = '';
  81.         }
  82.         $new_hot_threadlist[] = $mthread;
  83. }

  84. //今日发贴排行
  85. $tomonth=date(n);
  86. $todate=date(j);
  87. $toyear=date(Y);
  88. $time=mktime(0,0,0,$tomonth,$todate,$toyear);
  89. $query=$db->query("select count(pid) as num,authorid,author from $tablepre"."posts where dateline>=$time group by authorid order by num desc limit 0,10");
  90. while($result=$db->fetch_array($query)){
  91.         $poststar.="<a href=space.php?uid=".$result[authorid].">".cutstr($result[author],8)."</a><font color=red>[".$result[num]."]</font>&nbsp; ";
  92. }
  93. $allarray['new'] =$new_post_threadlist;
  94. $allarray['reply'] =$new_reply_threadlist;
  95. $allarray['hot'] =$new_hot_threadlist;
  96. $allarray['poststar'] =$poststar;
  97. cache_write('all',$allarray);
  98. //----首页五格代码结束
  99. }
  100. //将数据重新归位.
  101. $new_post_threadlist = $allarray['new'];
  102. $new_reply_threadlist = $allarray['reply'];
  103. $new_hot_threadlist = $allarray['hot'];
  104. $poststar = $allarray['poststar'];
复制代码
还有对首页版块的缓存:
  1.         $mysqlda = cache_read('all2',3600);
  2.         if(is_array($mysqlda)===false){
  3.         $sql = !empty($accessmasks) ?
  4.                                 "SELECT f.fid, f.fup, f.type, f.name, f.threads, f.posts, f.todayposts, f.lastpost, f.inheritedmod, f.forumcolumns, f.simple, ff.description, ff.moderators, ff.icon, ff.viewperm, ff.redirect, a.allowview FROM {$tablepre}forums f
  5.                                         LEFT JOIN {$tablepre}forumfields ff ON ff.fid=f.fid
  6.                                         LEFT JOIN {$tablepre}access a ON a.uid='$discuz_uid' AND a.fid=f.fid
  7.                                         WHERE f.status>0 ORDER BY f.type, f.displayorder"
  8.                                 : "SELECT f.fid, f.fup, f.type, f.name, f.threads, f.posts, f.todayposts, f.lastpost, f.inheritedmod, f.forumcolumns, f.simple, ff.description, ff.moderators, ff.icon, ff.viewperm, ff.redirect FROM {$tablepre}forums f
  9.                                         LEFT JOIN {$tablepre}forumfields ff USING(fid)
  10.                                         WHERE f.status>0 ORDER BY f.type, f.displayorder";

  11.         $query = $db->query($sql);

  12.         while($row = $db->fetch_array($query)) {
  13.                 $fourmsdata[] = $row;
  14.         }
  15.         $mysqlda['fourmsdata'] = $fourmsdata;
  16.         cache_write('all2',$mysqlda);
  17.         }
  18.         $fourmsdata = $mysqlda['fourmsdata'];
复制代码
现在看看底部, 会发现SQL查询从8次, 变成3次了.  速度肯定会加快.

举一反三, 许多数据都可以这样来缓存.  大家试验一下吧. 有什么问题, 回复.
回复

使用道具 举报

maikongjian14a 发表于 2009-5-5 09:36:32 | 显示全部楼层
收藏了,试验一下去
回复

使用道具 举报

习明 发表于 2009-5-5 09:40:36 | 显示全部楼层
直接用自带的数据调用就是了
回复

使用道具 举报

ydxiao 发表于 2009-5-5 15:49:54 | 显示全部楼层
好家伙,这东西用处很广.
回复

使用道具 举报

贵在要坚持 发表于 2009-5-5 16:29:47 | 显示全部楼层
不错,不过7.0自带的也不错
回复

使用道具 举报

ck丿沉珂 发表于 2009-5-5 16:41:55 | 显示全部楼层
支持,

没装4格,,
回复

使用道具 举报

11107207 发表于 2009-5-5 16:47:21 | 显示全部楼层
谢谢分享
回复

使用道具 举报

yilin101 发表于 2009-5-5 18:05:44 | 显示全部楼层
不是很明白
能详细点吗 谢谢
回复

使用道具 举报

philchu 发表于 2009-6-1 11:30:11 | 显示全部楼层
不是很明白.....有点迷茫.
用DZ的自带缓存怎么改?
回复

使用道具 举报

handasontam 发表于 2009-6-1 12:45:12 | 显示全部楼层
首页四格的代码放在那里??
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2025-9-13 06:19 , Processed in 0.109620 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表