|
- 仅使用3次查询, 即可以完成首页显示.
- disucz本身是有缓存系统的, 可缓存的过程被固定了, 没办法新增加缓存内容及方法. 为此, 写了个插件来支持无限的缓存意识, 让部分查询SQL可以稍休息片刻. 这样的作用在于,减轻SQL压力, 更快显示出内容.
- 安装方法:
- 首先下载function.rar,将里面的php文件上传在根目录.
- 下载
- 接着打开: index.php
- 在
- $discuz_action = 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,缓存文件名);
- 现在我们来优化首页四格.
- $allarray = cache_read('all',3600);
- if(is_array($allarray)===false){
- //----首页五格代码开始 缓存开始
- $colorarray = array('', 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'gray');
- //新贴
- $hack_cut_str = 28; //标题字数
- $hack_cut_strauthor = 9;
- $new_post_threadlist = array();
- $nthread = array();
- $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");
- while($nthread = $db->fetch_array($query)) {
- $nthread['forumname'] = ereg_replace('<[^>]*>','',$nthread['name']);
- $nthread['view_subject'] = cutstr($nthread['subject'],$hack_cut_str);
- $nthread['view_author'] = cutstr($nthread['author'],$hack_cut_strauthor);
- $nthread['date']= gmdate("$dateformat $timeformat", $nthread['dateline'] + $timeoffset * 3600);
- $nthread['lastreplytime']= gmdate("$dateformat $timeformat", $nthread[lastpost] + ($timeoffset * 3600));
- if($nthread['highlight']) {
- $string = sprintf('%02d', $nthread['highlight']);
- $stylestr = sprintf('%03b', $string[0]);
- $nthread['highlight'] = 'style="';
- $nthread['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
- $nthread['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
- $nthread['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
- $nthread['highlight'] .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';
- $nthread['highlight'] .= '"';
- } else {
- $nthread['highlight'] = '';
- }
- $new_post_threadlist[] = $nthread;
- }
- //新回复
- $hack_cut_str = 28; //标题字数
- $hack_cut_strauthor = 9;
- $new_reply_threadlist = array();
- $rthread = array();
- $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");
- while($rthread = $db->fetch_array($query)) {
- $rthread['forumname'] = ereg_replace('<[^>]*>','',$rthread['name']);
- $rthread['view_subject'] = cutstr($rthread['subject'],$hack_cut_str);
- $rthread['view_lastposter'] = cutstr($rthread['lastposter'],$hack_cut_strauthor);
- $rthread['date']= gmdate("$dateformat $timeformat", $rthread['dateline'] + $timeoffset * 3600);
- $rthread['lastreplytime']= gmdate("$dateformat $timeformat", $rthread[lastpost] + ($timeoffset * 3600));
- if($rthread['highlight']) {
- $string = sprintf('%02d', $rthread['highlight']);
- $stylestr = sprintf('%03b', $string[0]);
- $rthread['highlight'] = 'style="';
- $rthread['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
- $rthread['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
- $rthread['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
- $rthread['highlight'] .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';
- $rthread['highlight'] .= '"';
- } else {
- $rthread['highlight'] = '';
- }
- $new_reply_threadlist[] = $rthread;
- }
- //本月热帖
- $hack_cut_str = 30; //标题字数
- $hack_cut_strauthor = 9;
- $new_hot_threadlist = array();
- $mthread = array();
- $ctime=$timestamp-3600*24*30;//最后30是天数为本月
- $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");
- while($mthread = $db->fetch_array($query)) {
- $mthread['forumname'] = ereg_replace('<[^>]*>','',$mthread['name']);
- $mthread['view_subject'] = cutstr($mthread['subject'],$hack_cut_str);
- $mthread['view_lastposter'] = cutstr($mthread['lastposter'],$hack_cut_strauthor);
- $mthread['date']= gmdate("$dateformat $timeformat", $mthread['dateline'] + $timeoffset * 3600);
- $mthread['lastreplytime']= gmdate("$dateformat $timeformat", $mthread[lastpost] + ($timeoffset * 3600));
- if($mthread['highlight']) {
- $string = sprintf('%02d', $mthread['highlight']);
- $stylestr = sprintf('%03b', $string[0]);
- $mthread['highlight'] = 'style="';
- $mthread['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
- $mthread['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
- $mthread['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
- $mthread['highlight'] .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';
- $mthread['highlight'] .= '"';
- } else {
- $mthread['highlight'] = '';
- }
- $new_hot_threadlist[] = $mthread;
- }
- //今日发贴排行
- $tomonth=date(n);
- $todate=date(j);
- $toyear=date(Y);
- $time=mktime(0,0,0,$tomonth,$todate,$toyear);
- $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");
- while($result=$db->fetch_array($query)){
- $poststar.="<a href=space.php?uid=".$result[authorid].">".cutstr($result[author],8)."</a><font color=red>[".$result[num]."]</font> ";
- }
- $allarray['new'] =$new_post_threadlist;
- $allarray['reply'] =$new_reply_threadlist;
- $allarray['hot'] =$new_hot_threadlist;
- $allarray['poststar'] =$poststar;
- cache_write('all',$allarray);
- //----首页五格代码结束
- }
- //将数据重新归位.
- $new_post_threadlist = $allarray['new'];
- $new_reply_threadlist = $allarray['reply'];
- $new_hot_threadlist = $allarray['hot'];
- $poststar = $allarray['poststar'];
- 复制代码
- 还有对首页版块的缓存:
- $mysqlda = cache_read('all2',3600);
- if(is_array($mysqlda)===false){
- $sql = !empty($accessmasks) ?
- "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
- LEFT JOIN {$tablepre}forumfields ff ON ff.fid=f.fid
- LEFT JOIN {$tablepre}access a ON a.uid='$discuz_uid' AND a.fid=f.fid
- WHERE f.status>0 ORDER BY f.type, f.displayorder"
- : "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
- LEFT JOIN {$tablepre}forumfields ff USING(fid)
- WHERE f.status>0 ORDER BY f.type, f.displayorder";
- $query = $db->query($sql);
- while($row = $db->fetch_array($query)) {
- $fourmsdata[] = $row;
- }
- $mysqlda['fourmsdata'] = $fourmsdata;
- cache_write('all2',$mysqlda);
- }
- $fourmsdata = $mysqlda['fourmsdata'];
- 复制代码
- 现在看看底部, 会发现SQL查询从8次, 变成3次了. 速度肯定会加快.
- 举一反三, 许多数据都可以这样来缓存.
复制代码
|
|