Mail Index


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ApacheGallery] patch to do proper Text::Template error reporting



On Fri, 05 Nov 2004, Don Armstrong wrote:
> Ideally we would read Template files just once when the instance is
> first run, but we'd have to cache based on
> $r->dir_config('GalleryTemplateDir'), and I'm not sure if that's
> worth the memory cost. [But if you're only using one set of
> templates...]  Anyway, if substantial numbers of people feel that
> this is worth the effort, it's not that difficult to implement.

Just for fun, I've implemented this against the current SVN head.

It doesn't really seem to have much effect here, but it's possible
that others would see a minor difference. [Most likely those template
files never actually make it out of the fs cache anyway, so a read on
them isn't very expensive.]

Anyway, if someone wants to play with it to see if it makes sense,
that would be cool. Maybe if I get more unlazy, I'll make some
semi-quantitative benchmarks. [I'd suggest using time httpd -X; and
probably running something like for a in $(seq 1 10000); do wget
--spider localhost; done; kill off httpd, then see how much user/sys
it took.]

I'm running this patch on gallery.donarmstrong.com, and it seems to
work for me, but try it yourself if it's interesting of course.


Don Armstrong

-- 
What I can't stand is the feeling that my brain is leaving me for 
someone more interesting.

http://www.donarmstrong.com              http://rzlab.ucr.edu
Index: lib/Apache/Gallery.pm
===================================================================
--- lib/Apache/Gallery.pm	(revision 288)
+++ lib/Apache/Gallery.pm	(working copy)
@@ -59,6 +59,9 @@
 my $escape_rule = "^A-Za-z0-9\-_.!~*'()\/";
 my $memoized;
 
+# Hashref to store cached templates by name
+my $cached_templates;
+
 sub handler {
 
 	my $r = shift or Apache->request();
@@ -194,7 +197,9 @@
 						  file      => "$tpl_dir/file.tpl",
 						  comment   => "$tpl_dir/dircomment.tpl",
 						  nocomment => "$tpl_dir/nodircomment.tpl",
-						 });
+						 },
+						 $r->dir_config('GalleryCacheTemplates'),
+						);
 
 
 
@@ -517,7 +522,9 @@
 						  slideshowoff   => "$tpl_dir/slideshowoff.tpl",
 						  pictureinfo    => "$tpl_dir/pictureinfo.tpl",
 						  nopictureinfo  => "$tpl_dir/nopictureinfo.tpl",
-						 });
+						 },
+						 $r->dir_config('GalleryCacheTemplates'),
+						);
 
 		my %tpl_vars;
 
@@ -1431,6 +1438,8 @@
 sub create_templates {
      my $templates = shift;
 
+     my $do_caching = shift || 0;
+
      # This routine is called whenever a template has an error. Prints
      # the error to STDERR and sticks the error in the output
      sub tt_broken {
@@ -1448,12 +1457,19 @@
      my %texttemplate_objects;
 
      for my $template_name (keys %$templates) {
-	  my $tt_obj = Text::Template->new(TYPE   => 'FILE',
-					   SOURCE => $$templates{$template_name},
-					   BROKEN => \&tt_broken,
-					   BROKEN_ARG => [$template_name, $$templates{$template_name}],
- 					  )
-	       or die "Unable to create new Text::Template object for $template_name: $Text::Template::ERROR";
+	  my $tt_obj;
+	  if ($do_caching and exists $cached_templates->{$templates->{$template_name}}) {
+	       $tt_obj = $cached_templates->{$templates->{$template_name}};
+	  }
+	  else {
+	       $tt_obj = Text::Template->new(TYPE   => 'FILE',
+					     SOURCE => $templates->{$template_name},
+					     BROKEN => \&tt_broken,
+					     BROKEN_ARG => [$template_name, $templates->{$template_name}],
+					    )
+		    or die "Unable to create new Text::Template object for $template_name: $Text::Template::ERROR";
+	       $cached_templates->{$templates->{$template_name}} = $tt_obj if $do_caching;
+	  }
 	  $texttemplate_objects{$template_name} = $tt_obj;
      }
      return %texttemplate_objects;
@@ -1516,6 +1532,16 @@
 
 No default value, this option is required.
 
+=item B<GalleryCacheTemplates>
+
+The templates used to generate the html layouts that Apache::Gallery
+uses can be cached instead of being read on every visit. Enabling this
+option may speed up accesses slightly at the expense of memory. If you
+enable this option and wish changes to templates to take effect, you
+must restart the server.
+
+The default is 0, or no caching. Set to 1 to enable caching.
+
 =item B<GalleryInfo>
 
 With this option you can define which EXIF information you would like