低配服务器循环二级栏目和内容打开非常慢的解决办法。原来TTFB2.6秒,现在600毫秒。

664次浏览 更新日期:2022-09-03 17:50:00 分类:程序交流 评论:3
源代码,循环了11遍。
{php $data = get_childcat(1);}
	{loop $data $val}
                <div class="index_st_list none">
                    <ul>
                    {m:lists field="title,url,yangyin,laoshi" catid="$val['catid']" limit="15" order="id DESC"}
			{loop $data $v}
                             <li>
                                <div class="fl index_st_list_head">
									<img src="<?php echo get_category($v['laoshi'], 'catimg');?>" alt="{$v[title]}"/>
                                </div>
                                <div class="fr index_st_list_txt">
                                    <p><a href="{$v[url]}">{$v[title]}</a></p>
					<div class="play paused" >
					<audio preload="none" src="{$v[yangyin]}"></audio>
					</div>
                                    <span><a href="<?php echo get_category($v['laoshi'], 'pclink');?>">{php echo get_catname($v['laoshi']);}</a></span>
                                </div>
                            </li>
			{/loop}  
                    </ul>
                    <div class="index_st_list_more"><a href="{get_category($val['catid'], 'pclink')}">查看更多</a></div>
                </div>
	{/loop}

仔细写个自定义函数 限制调用字段,我需要四个字段catid,pclink,catname,catimg

/*获取栏目信息*/
function get_cat($id,$fi){
$where = 'catid ='.$id;
    $d = D('category')->field('catid,pclink,catname,catimg')->where($where)->select();
    foreach ($d as $v){
        return $v["$fi"];
    }
}

修改后代码

   {m:nav field="catid,pclink" where="parentid=1" limit="15"}
   {loop $data $val}
             <div class="index_st_list none">
                 <ul>
                 {m:lists field="title,thumb,url,yangyin,laoshi" catid="$val['catid']" limit="15" order="RAND()"}
      {loop $data $v}
                          <li>
                             <div class="fl index_st_list_head">
                  <img src="{get_cat($v['laoshi'],'catimg')}" alt="{$v[title]}"/>
                             </div>
                             <div class="fr index_st_list_txt">
                                 <p><a href="{$v[url]}">{$v[title]}</a></p>
                  <div class="play paused" >
                    <audio preload="none" src="{$v[yangyin]}"></audio>
                  </div>
                                 <span><a href="{get_cat($v['laoshi'],'pclink')}">{get_cat($v['laoshi'],'catname')}</a></span>
                             </div>
                         </li>
       {/loop}
                 </ul>
                 <div class="index_st_list_more"><a href="{$val['pclink']}">查看更多</a></div>
             </div>
{/loop}

这样调用也会慢:

{get_category($val['catid'], 'pclink')}

改成

$val['pclink']}


以前用虚拟主机的时候打开是不慢的。可能换了服务器配置不太好,打开速度很慢。瞎弄了一天,现在可以对付了。



我来说两句
作者信息
发布见解
发内容 回顶部