OSDN Git Service

linkプラグインは1リクエストの処理内ではパース結果をキャッシュして負荷を減らすようにした。
authortakezoe <takezoe@871d6764-1e22-0410-b313-a55050885396>
Fri, 23 Dec 2011 18:02:38 +0000 (18:02 +0000)
committertakezoe <takezoe@871d6764-1e22-0410-b313-a55050885396>
Fri, 23 Dec 2011 18:02:38 +0000 (18:02 +0000)
takezoe/plugin/book/Link.pm

index 7621bf1..16a9050 100644 (file)
@@ -20,6 +20,7 @@ package plugin::book::Link;
 sub new {
        my $class = shift;
        my $self = {};
+       my $self->{cache} = {};
        return bless $self,$class;
 }
 
@@ -35,61 +36,62 @@ sub inline {
        $page = $wiki->get_CGI->param('page') unless $page;
        my $source = $wiki->get_page($page);
        
-       my $text = '';
-       
-       my @lines = split(/\n/,$wiki->get_page($page));
-       my $chapter = '';
-       my $count = {};
-       my $title1Count = 0;
-       my $title2Count = 0;
-       my $title3Count = 0;
+       unless(defined($self->{cache}->{$page})){
+               $self->{cache}->{$page} = {};
+               my $source = $wiki->get_page($page);
+
+               my $text = '';
        
-       # TODO {{pre}}¥×¥é¥°¥¤¥óÆâ¤Îµ­½Ò¤ÏÈô¤Ð¤µ¤Ê¤¤¤È¥À¥á¡ª¡ª¥Ñ¡¼¥µ¤òºî¤é¤Ê¤¤¤È¥À¥á¤Ã¤Ý¤¤¡©
-       foreach my $line (@lines){
-               if($line =~ /^{{(chapter.+}})$/){
-                       my $plugin = $wiki->parse_inline_plugin($1);
-                       $chapter = $plugin->{'args'}->[0];
-                       $title1Count = 0;
-                       $title2Count = 0;
-                       $title3Count = 0;
-                       foreach my $key (keys(%$count)){
-                               $count->{$key} = 0;
-                       }
-               } elsif($line =~ /^!!!{{(title1.+}})(.+)$/){
-                       my $plugin = $wiki->parse_inline_plugin($1);
-                       $title1Count++;
-                       $title2Count = 0;
-                       $title3Count = 0;
-                       if($plugin->{'args'}->[0] eq $anchor){
+               my @lines = split(/\n/,$wiki->get_page($page));
+               my $chapter = '';
+               my $count = {};
+               my $title1Count = 0;
+               my $title2Count = 0;
+               my $title3Count = 0;
+               
+               # TODO {{pre}}¥×¥é¥°¥¤¥óÆâ¤Îµ­½Ò¤ÏÈô¤Ð¤µ¤Ê¤¤¤È¥À¥á¡ª¡ª¥Ñ¡¼¥µ¤òºî¤é¤Ê¤¤¤È¥À¥á¤Ã¤Ý¤¤¡©
+               foreach my $line (@lines){
+                       if($line =~ /^{{(chapter.+}})$/){
+                               my $plugin = $wiki->parse_inline_plugin($1);
+                               $chapter = $plugin->{'args'}->[0];
+                               $title1Count = 0;
+                               $title2Count = 0;
+                               $title3Count = 0;
+                               foreach my $key (keys(%$count)){
+                                       $count->{$key} = 0;
+                               }
+                       } elsif($line =~ /^!!!{{(title1.+}})(.+)$/){
+                               my $plugin = $wiki->parse_inline_plugin($1);
+                               $title1Count++;
+                               $title2Count = 0;
+                               $title3Count = 0;
                                $text= $chapter.'-'.$title1Count.'. '.Util::trim($2);
-                               last;
-                       }
-               } elsif($line =~ /^!!{{(title2.+}})$/){
-                       my $plugin = $wiki->parse_inline_plugin($1);
-                       $title2Count++;
-                       $title3Count = 0;
-                       if($plugin->{'args'}->[0] eq $anchor){
+                               $self->{cache}->{$page}->{$plugin->{'args'}->[0]} = $text;
+                               
+                       } elsif($line =~ /^!!{{(title2.+}})$/){
+                               my $plugin = $wiki->parse_inline_plugin($1);
+                               $title2Count++;
+                               $title3Count = 0;
                                $text= $chapter.'-'.$title1Count.'-'.$title2Count.'. '.Util::trim($2);
-                               last;
-                       }
-               } elsif($line =~ /^!{{(title3.+}})$/){
-                       my $plugin = $wiki->parse_inline_plugin($1);
-                       $title3Count++;
-                       if($plugin->{'args'}->[0] eq $anchor){
+                               $self->{cache}->{$page}->{$plugin->{'args'}->[0]} = $text;
+                               
+                       } elsif($line =~ /^!{{(title3.+}})$/){
+                               my $plugin = $wiki->parse_inline_plugin($1);
+                               $title3Count++;
                                $text= $chapter.'-'.$title1Count.'-'.$title2Count.'-'.$title3Count.'. '.Util::trim($2);
-                               last;
-                       }
-               } elsif($line =~ /^{{(caption.+}})$/){
-                       my $plugin = $wiki->parse_inline_plugin($1);
-                       my $type = $plugin->{'args'}->[0];
-                       $count->{$type}++;
-                       if($plugin->{'args'}->[2] eq $anchor){
+                               $self->{cache}->{$page}->{$plugin->{'args'}->[0]} = $text;
+                               
+                       } elsif($line =~ /^{{(caption.+}})$/){
+                               my $plugin = $wiki->parse_inline_plugin($1);
+                               my $type = $plugin->{'args'}->[0];
+                               $count->{$type}++;
                                $text = $type.$chapter.'-'.$count->{$type}.': '.Util::trim($plugin->{'args'}->[1]);
-                               last;
+                               $self->{cache}->{$page}->{$plugin->{'args'}->[2]} = $text;
                        }
                }
        }
-       if($text ne ''){
+       if(defined($self->{cache}->{$page}->{$anchor})){
+               my $text = $self->{cache}->{$page}->{$anchor};
                return '<a href="?page='.Util::url_encode($page).'#'.Util::escapeHTML($anchor).'" class="xref">'.Util::escapeHTML($text).'</a>';
        } else {
                return '<span class="xref-error">»²¾ÈÀ褬¸«¤Ä¤«¤ê¤Þ¤»¤ó¡ª</span>';