如何泡制具有缓存功能的MT查询模块?

    将Search.pm拷贝成NewSearch.pm.

    将NewSearch.pm中的所有"MT::App::Search"替换成"MT::App::NewSearch".

    在execute这个子函数前面加一个函数:

sub getCacheFileName {
    my($str) = @_;
    $str =~ s!(.)!uc sprintf "%02x", ord($1)!eg;
    $str;
}

    在execute函数的前面部份加上如下红色部份代码:

    return $app->error($app->errstr) if $app->errstr;
    my $cachefile = ($ENV{MT_HOME} ? "$ENV{MT_HOME}/searchcache" : 'searchcache')
              ."/tags_".$app->param("blog_id")."_"
              .getCacheFileName($app->param("search")).".txt";
    if ($app->param("blog_id") && $app->{searchparam}{Type} eq 'tag')
    {
       if (-e $cachefile)
       {
         if( (time() - ((stat $cachefile)[9])) < (3600 * 48))
         {
           open IMGOUT, "<".$cachefile;
           binmode IMGOUT;
           my $res = "";
           my $text = <IMGOUT>;
           while ($text){
             $res .= $text;
             $text = <IMGOUT>;
           }
           close IMGOUT;  
           return $res;
         }
       }
    }

    在execute函数的最后部份加上如下红色部份代码:

    if ($app->param("blog_id") && $app->{searchparam}{Type} eq 'tag')
    {
       open IMGOUT, ">".$cachefile;
       print IMGOUT $res;
       close IMGOUT;
    }
        
    $res;

    当MT的版本更新后, 请大家自已泡制具有缓存功能的MT查询模块吧!

发表留言: