Changeset 119
- Timestamp:
- 04/24/07 22:33:58 (3 years ago)
- Location:
- veekun/trunk
- Files:
-
- 2 modified
-
lib/Vee/Controller/Root.pm (modified) (4 diffs)
-
templates/wrapper.tt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
veekun/trunk/lib/Vee/Controller/Root.pm
r118 r119 97 97 98 98 # capture flagrant errors in prod 99 if ( !$c->debug and scalar @{ $c->error }) {99 if (not $c->debug and scalar @{ $c->error }) { 100 100 eval { 101 101 $c->model('DBIC::ErrorLog')->create({ … … 156 156 } 157 157 158 # if _cache holds a path, grab the output manually and cache it to disk158 # if _cache_fh is set, grab the output manually and cache it to disk 159 159 # 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}) { 165 161 # disable wrapper, then get just the body 166 162 my $skip_wrapper = $s->{skip_wrapper}; … … 169 165 170 166 # cache 167 my $cache_file = $s->{_cache_fh}; 171 168 print $cache_file $body; 169 flock $cache_file, 8; 172 170 close $cache_file; 173 171 174 # let the RenderView render the newly-made cached file175 172 $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'); 178 178 } 179 179 } … … 191 191 my $s = $c->stash; 192 192 193 # return if $c->debug; 194 return if not $s->{template}; 195 193 196 $key = '' if not defined $key; 194 197 $key =~ tr/-_a-zA-Z0-9//cd; 195 198 $key = 'default' if not length $key; 196 199 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}; 200 202 201 203 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 204 205 $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; 210 207 } else { 211 208 # 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} ) }; 213 210 return if $@; 214 211 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 221 Used to... render a template from the cache. Mostly used as a hack to avoid 222 template compilation, but I'd also like to figure out how to get TT to simply 223 not parse the cached file at all. 224 225 =cut 226 227 sub 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 } 227 235 228 236 =head1 AUTHOR -
veekun/trunk/templates/wrapper.tt
r117 r119 143 143 All content copyright its original owners. Pokémon is copyright to Nintendo and Game Freak. Current site layout by Eevee/Alex Munroe and is ©2005. <br/> 144 144 [% IF from_cache %] 145 This page was retrieved from cache by a team of <a href="[% uri('Dex', 'pokemon', 'scyther') %]">Scyther</a> jo nin 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/> 146 146 [% ELSE %] 147 147 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/>
