Changeset 119

Show
Ignore:
Timestamp:
04/24/07 22:33:58 (3 years ago)
Author:
eevee
Message:

Fixed some caching problems, mostly revolving around COMPILE_DIR applying to the cached templates.

Location:
veekun/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • veekun/trunk/lib/Vee/Controller/Root.pm

    r118 r119  
    9797 
    9898    # capture flagrant errors in prod 
    99     if (!$c->debug and scalar @{ $c->error }) { 
     99    if (not $c->debug and scalar @{ $c->error }) { 
    100100        eval { 
    101101            $c->model('DBIC::ErrorLog')->create({ 
     
    156156    } 
    157157 
    158     # if _cache holds a path, grab the output manually and cache it to disk 
     158    # if _cache_fh is set, grab the output manually and cache it to disk 
    159159    # NOTE: THIS EXPECTS THAT A WRAPPER IS USED 
    160     if ($s->{_cache}) { 
    161         open my $cache_file, '>', $c->path_to( 'tmp', 'cache', $s->{_cache} ); 
    162  
    163         # HAY: print meta to cache here 
    164  
     160    if ($s->{_cache_fh}) { 
    165161        # disable wrapper, then get just the body 
    166162        my $skip_wrapper = $s->{skip_wrapper}; 
     
    169165 
    170166        # cache 
     167        my $cache_file = $s->{_cache_fh}; 
    171168        print $cache_file $body; 
     169        flock $cache_file, 8; 
    172170        close $cache_file; 
    173171 
    174         # let the RenderView render the newly-made cached file 
    175172        $s->{skip_wrapper} = $skip_wrapper; 
    176         $s->{template} = $s->{_cache}; 
    177         push @{$s->{additional_template_paths}}, $c->path_to(qw/ tmp cache /); 
     173        $c->forward('/render_from_cache'); 
     174 
     175    # this is done here so the above querylog/etc code can run first 
     176    } elsif ($s->{from_cache}) { 
     177        $c->forward('/render_from_cache'); 
    178178    } 
    179179} 
     
    191191    my $s = $c->stash; 
    192192 
     193#    return if $c->debug; 
     194    return if not $s->{template}; 
     195 
    193196    $key = '' if not defined $key; 
    194197    $key =~ tr/-_a-zA-Z0-9//cd; 
    195198    $key = 'default' if not length $key; 
    196199 
    197     return if not $s->{template}; 
    198  
    199     my $cache_path = $c->path_to( 'tmp', 'cache', $s->{template}, $key ); 
     200    $s->{_cache_file} = "$s->{template}/$key"; 
     201    my $cache_path = $c->path_to(qw/ tmp cache /) . '/' . $s->{_cache_file}; 
    200202 
    201203    if (-e $cache_path) { 
    202         # if the cache file exists, spit it out 
    203         $s->{template} = "$s->{template}/$key"; 
     204        # if the cache file exists, flag end() to spit it out 
    204205        $s->{from_cache} = 1; 
    205         push @{$s->{additional_template_paths}}, $c->path_to(qw/ tmp cache /); 
    206  
    207         # ...then skip the rest of the calling controller 
    208         # TODO: should we treat the cache as flat text, read it ourselves..? 
    209         $c->detach('/null'); 
     206        $c->detach; 
    210207    } else { 
    211208        # create directory tree; if there be a problem, just don't cache 
    212         eval { mkpath( $c->path_to(qw/ tmp cache /) . "/$s->{template}" ) }; 
     209        eval { mkpath( $c->path_to(qw/ tmp cache /) . '/' . $s->{template} ) }; 
    213210        return if $@; 
    214211 
    215         $s->{_cache} = "$s->{template}/$key"; 
    216     } 
    217 } 
    218  
    219 =head2 null 
    220  
    221 Does nothing.  Used as an endpoint for /cache.  Probably a better way to do 
    222 this. 
    223  
    224 =cut 
    225  
    226 sub null : Private { ; } 
     212        # open here and flock to prevent race condition 
     213        open my $cache_file, '>', $c->path_to(qw/ tmp cache /) . '/' . $s->{_cache_file}; 
     214        flock $cache_file, 2; 
     215        $s->{_cache_fh} = $cache_file; 
     216    } 
     217} 
     218 
     219=head2 render_from_cache 
     220 
     221Used to...  render a template from the cache.  Mostly used as a hack to avoid 
     222template compilation, but I'd also like to figure out how to get TT to simply 
     223not parse the cached file at all. 
     224 
     225=cut 
     226 
     227sub render_from_cache : Private { 
     228    my ($self, $c) = @_; 
     229    my $s = $c->stash; 
     230 
     231    open my $cache_file, '<', $c->path_to(qw/ tmp cache /, $s->{_cache_file} ); 
     232    $c->res->body( $c->view->render($c, $cache_file) ); 
     233    close $cache_file; 
     234} 
    227235 
    228236=head1 AUTHOR 
  • veekun/trunk/templates/wrapper.tt

    r117 r119  
    143143    All content copyright its original owners.  Pok&eacute;mon is copyright to Nintendo and Game Freak.  Current site layout by Eevee/Alex Munroe and is &copy;2005. <br/> 
    144144[% IF from_cache %] 
    145     This page was retrieved from cache by a team of <a href="[% uri('Dex', 'pokemon', 'scyther') %]">Scyther</a> jonin in about [% time_delta = time - start_time; time_delta | format('%0.6f') %] seconds. <br/> 
     145    This page was retrieved from cache by a team of <a href="[% uri('Dex', 'pokemon', 'scyther') %]">Scyther</a> jounin in about [% time_delta = time - start_time; time_delta | format('%0.6f') %] seconds. <br/> 
    146146[% ELSE %] 
    147147    This page was created by a team of trained <a href="[% uri('Dex', 'pokemon', 'ambipom') %]">Ambipom</a> in about [% time_delta = time - start_time; time_delta | format('%0.6f') %] seconds. <br/>