本帖最后由 littlehz 于 2009-11-30 10:42 编辑
本调用代码用于放置在首页中,列表模块带缓存,每个分页的缓存时间相差20秒,避免同一时间所有缓存全部失效,某一时刻非常缓慢的问题。列表数量统计的模块也带缓存。
取所有频道的资讯文章,有缩略图显示缩略图,无缩略图显示无缩略图的文件。
对比嵌套循环查询,极大减少数据库查询次数(嵌套查询可能达到11次数据库查询),本方法只需查询一次数据库。
列表模块对应的SQL语句为- SELECT i.itemid, i.catid, i.uid, i.username, i.subject, i.dateline, i.viewnum, i.replynum, i.digest, i.top, i.picid, c.name, n.message, a.thumbpath, a.subject AS a_subject
- FROM supe_spaceitems i
- INNER JOIN supe_spacenews n ON i.itemid = n.itemid
- INNER JOIN supe_categories c ON i.catid = c.catid
- LEFT JOIN supe_attachments a ON i.picid = a.aid
- WHERE i.type='news'
- ORDER BY i.top DESC, i.itemid DESC
复制代码 两个INNER JOIN内联一个LEFT JOIN左连接做四表联查,全部为主键或者索引查询,相对效率高。
由于用了联查,block模块中的perpage分页代码无效,只能自己调用multi函数处理分页内容。分页数量统计对应的SQL语句为- SELECT count( itemid ) AS total FROM `supe_spaceitems` WHERE type='news'
复制代码 cachename缓存以mynews_pagexx命名的变量,解决缓存覆盖问题。
演示:http://www.littz.cn,SupeSite 7.5,256M内存VPS服务器在每秒1000次并发压力下,关闭缓存平均页面执行时间为0.08秒,开启缓存平均0.006秒执行时间。
完整调用代码如下:- <!-- Article begin -->
- <div class="article">
- <!--{eval $basecachetime = 3500; /*设置本页面基础缓存时间*/}-->
- <!--{eval $baseperpage = 10; /*设置本页面的单页文章数量*/}-->
- <!--{eval $mypage = $_SGET['page']?$_SGET['page']:1;}-->
- <!--{eval $mystart = ($mypage-1)*$baseperpage}-->
- <!--{eval $mycachename = 'mynews_page'.$mypage}-->
- <!--{eval $mycachetime = $basecachetime + $mystart*2}-->
- <!--{block name="spacenews" parameter="type/news/sql/SELECT%20i.itemid%2C%20i.catid%2C%20i.uid%2C%20i.username%2C%20i.subject%2C%20i.dateline%2C%20i.viewnum%2C%20i.replynum%2C%20i.digest%2C%20i.top%2C%20i.picid%2C%20c.name%2C%20n.message%2C%20a.thumbpath%2C%20a.subject%20AS%20a_subject%0D%0AFROM%20supe_spaceitems%20i%0D%0AINNER%20JOIN%20supe_spacenews%20n%20ON%20i.itemid%20%3D%20n.itemid%0D%0AINNER%20JOIN%20supe_categories%20c%20ON%20i.catid%20%3D%20c.catid%0D%0ALEFT%20JOIN%20supe_attachments%20a%20ON%20i.picid%20%3D%20a.aid%0D%0AORDER%20BY%20i.top%20DESC%2C%20i.itemid%20DESC/limit/$mystart,$baseperpage/cachename/$mycachename/cachetime/$mycachetime/tpl/data"}--><!--Thiese Block had cached in dollar_SBLOCK[$mycachename]-->
- <!--{loop $_SBLOCK[$mycachename] $value}-->
- <!--{eval $value['message'] = trim(strip_tags($value['message']));}-->
- <!--{eval $value['message'] = trim(cutstr($value['message'], '290', '1'));}-->
- <!--{if $value['top']}-->
- <!--{eval $value['subject'] = '[置顶'.$value['top'].'] '.$value['subject'];}-->
- <!--{/if}-->
- <!--{if $value['digest']}-->
- <!--{eval $value['subject'] .= ' [精华'.$value['digest'].']';}-->
- <!--{/if}-->
- <div class="post">
- <h2><a href="$value['url']" title="$value['subject']">$value['subject']</a></h2>
- <span class="pcomments"><em>$value['replynum']</em>个评论</span>
- <div class="clear"></div>
- <!--{if $value['thumbpath']}-->
- <a href="$value['url']"><img src="$_SSCONFIG['attachmenturl']/$value['thumbpath']" title="$value['a_subject']" alt="$value['a_subject']" class="thumb_image" /></a>
- <!--{else}-->
- <a href="$value['url']"><img src="{S_URL}/templates/$_SCONFIG[template]/images/no-thumb.jpg" title="$value['subject']" alt="$value['subject']" class="thumb_image" /></a>
- <!--{/if}-->
- <div class="pcontent">
- $value['message']
- </div>
- <div class="clear"></div>
- <div class="pmeta">
- #date('Y-m-d',$value['dateline'])# / <a href="#action/category/catid/$value['catid']#" title="$value['name']">$value['name']</a> / $value['viewnum']浏览
- </div>
- <a href="$value['url']" title="阅读全文" class="readmore">阅读全文</a>
- </div>
- <!--{/loop}-->
- <!-- Navigation begin -->
- <div class="page_navi">
- <!--翻页输出控制-->
- <!--{eval $mylistcount = 1;}-->
- <!--{block name="category" parameter="sql/SELECT%20count%28%20itemid%20%29%20AS%20total%20FROM%20%60supe_spaceitems%60/limit/1/cachetime/$basecachetime/cachename/indexcount/tpl/data"}--><!--Index listcount has cached in mylistcount-->
- <!--{eval $mylistcount = $_SBLOCK['indexcount'][0]['total'];}-->
- <!--{if $mylistcount}-->
- <!--{eval $_SGET['action'] = 'index';}-->
- <!--{eval $myurlarr = $_SGET;}-->
- <!--{eval unset($myurlarr['page']);}-->
- <!--{eval $_SBLOCK['mynews_multipage'] = multi($mylistcount, $baseperpage, $mypage, $myurlarr, 0);}-->
- <!--{/if}-->
- <!--{if $_SBLOCK['mynews_multipage']}-->
- $_SBLOCK['mynews_multipage']
- <!--{/if}-->
- </div>
- <!-- Navigation end -->
- </div>
- <!-- Article end -->
复制代码 |