[MKDoc-commit] [Petal] CACHE_ONLY option (Jonathan Vanasco)

bruno at mkdoc.demon.co.uk bruno at mkdoc.demon.co.uk
Thu Mar 2 16:43:40 GMT 2006


Log Message:
-----------
[Petal] CACHE_ONLY option (Jonathan Vanasco)

Modified Files:
--------------
    Petal:
        Changes
    Petal/lib:
        Petal.pm
    Petal/t:
        054_Opt_cache.t

Added Files:
-----------
    Petal/t:
        097_cache_only.t

-------------- next part --------------
Index: Changes
===================================================================
RCS file: /var/spool/cvs/Petal/Changes,v
retrieving revision 1.147
retrieving revision 1.148
diff -LChanges -LChanges -u -r1.147 -r1.148
--- Changes
+++ Changes
@@ -4,6 +4,7 @@
     - alias false: modifier as not: to match TALES spec (Jonathan Vanasco)
     - fix repeat/odd documentation bug and add to cookbook (Jonathan Vanasco)
     - pod typos (sarabob)
+    - CACHE_ONLY option (Jonathan Vanasco)
 
 2.18 Tue Jan 17 13:01:00 2006
     - $Petal::CodeGenerator::PI_RE should be applied newline insensitively
Index: Petal.pm
===================================================================
RCS file: /var/spool/cvs/Petal/lib/Petal.pm,v
retrieving revision 1.142
retrieving revision 1.143
diff -Llib/Petal.pm -Llib/Petal.pm -u -r1.142 -r1.143
--- lib/Petal.pm
+++ lib/Petal.pm
@@ -95,6 +95,10 @@
 our $MEMORY_CACHE = 1;
 
 
+# cache only mode
+our $CACHE_ONLY = 0;
+
+
 # prevents infinites includes...
 our $MAX_INCLUDES = 30;
 our $CURRENT_INCLUDES = 0;
@@ -137,6 +141,7 @@
     my $file = shift (@ARGV);
     local $Petal::DISK_CACHE = 0;
     local $Petal::MEMORY_CACHE = 0;
+    local $Petal::CACHE_ONLY = 0;
     local $Petal::INPUT  = $ENV{PETAL_INPUT}  || 'XML';
     local $Petal::OUTPUT = $ENV{PETAL_OUTPUT} || 'XHTML';
     print ${Petal->new ($file)->_canonicalize()};
@@ -151,6 +156,7 @@
     my $file = shift (@ARGV);
     local $Petal::DISK_CACHE = 0;
     local $Petal::MEMORY_CACHE = 0;
+    local $Petal::CACHE_ONLY = 0;
     print Petal->new ($file)->_code_disk_cached;
 }
 
@@ -163,6 +169,7 @@
     my $file = shift (@ARGV);
     local $Petal::DISK_CACHE = 0;
     local $Petal::MEMORY_CACHE = 0;
+    local $Petal::CACHE_ONLY = 0;
     print Petal->new ($file)->_code_with_line_numbers;
 }
 
@@ -241,6 +248,7 @@
 sub error_on_undef_var { exists $_[0]->{error_on_undef_var} ? $_[0]->{error_on_undef_var} : $ERROR_ON_UNDEF_VAR }
 sub disk_cache         { exists $_[0]->{disk_cache}         ? $_[0]->{disk_cache}         : $DISK_CACHE         }
 sub memory_cache       { exists $_[0]->{memory_cache}       ? $_[0]->{memory_cache}       : $MEMORY_CACHE       }
+sub cache_only         { exists $_[0]->{cache_only}         ? $_[0]->{cache_only}         : $CACHE_ONLY         }
 sub max_includes       { exists $_[0]->{max_includes}       ? $_[0]->{max_includes}       : $MAX_INCLUDES       }
 
 
@@ -341,6 +349,7 @@
     local $ERROR_ON_UNDEF_VAR = defined $self->{error_on_undef_var}  ? $self->{error_on_undef_var}  : $ERROR_ON_UNDEF_VAR;
     local $DISK_CACHE         = defined $self->{disk_cache}          ? $self->{disk_cache}          : $DISK_CACHE;
     local $MEMORY_CACHE       = defined $self->{memory_cache}        ? $self->{memory_cache}        : $MEMORY_CACHE;
+    local $CACHE_ONLY         = defined $self->{cache_only}          ? $self->{cache_only}          : $CACHE_ONLY;
     local $MAX_INCLUDES       = defined $self->{max_includes}        ? $self->{max_includes}        : $MAX_INCLUDES;
     local $INPUT              = defined $self->{input}               ? $self->{input}               : $INPUT;
     local $OUTPUT             = defined $self->{output}              ? $self->{output}              : $OUTPUT;
@@ -369,6 +378,8 @@
 	die "\$hash is undefined\n\n" unless $hash;
 	$res = $coderef->($hash);
     };
+    
+    if ( $CACHE_ONLY == 1 ){ return 1; }
    
     if (defined $@ and $@) { $res = $self->_handle_error ($@) }
     elsif (defined $TranslationService && $CURRENT_INCLUDES == 1) { $res = Petal::I18N->process ($res) } 
@@ -964,6 +975,13 @@
 If set to C<false>, Petal will not use the C<Petal::Cache::Memory> module.
 
 
+=head2 cache_only => I<true> | I<false> (default: I<false>)
+
+If set to C<true>, Petal will return true after having compiled a template into
+perl code and a subroutine , and optionally using disk_cache or memory_cache if
+either is set.
+
+
 =head2 max_includes => I<number> (default: 30)
 
 The maximum number of recursive includes before Petal stops processing.  This
@@ -1733,6 +1751,10 @@
 Otherwise, subsequent calls will resume at step 6, until the source template
 changes.
 
+If you are using the mod_perl prefork MPM, you can precompile Petal templates
+into Apache's shared memory at startup by using the cache_only option.  This
+will allow you to run through steps 1-7 without passing any data to Petal.
+
 
 =head1 DECRYPTING WARNINGS AND ERRORS
 
Index: 054_Opt_cache.t
===================================================================
RCS file: /var/spool/cvs/Petal/t/054_Opt_cache.t,v
retrieving revision 1.3
retrieving revision 1.4
diff -Lt/054_Opt_cache.t -Lt/054_Opt_cache.t -u -r1.3 -r1.4
--- t/054_Opt_cache.t
+++ t/054_Opt_cache.t
@@ -3,7 +3,7 @@
 # Tests the 'disk_cache' and 'memory_cache' options to Petal->new.
 #
 
-use Test::More tests => 8;
+use Test::More tests => 12;
 
 use warnings;
 use lib 'lib';
@@ -73,3 +73,33 @@
 ok($template->memory_cache, "memory_cache option turns it on again");
 
 
+
+# Confirm cache_only defaults to off
+
+$template = new Petal (file => $file, base_dir => $data_dir);
+
+ok(!$template->cache_only, "cache_only defaults to off");
+
+
+# Confirm option can enable it
+
+$template = new Petal (file => $file, base_dir => $data_dir, cache_only => 1);
+
+ok($template->cache_only, "cache_only option turns it on");
+
+
+# Confirm global can enable it
+
+$Petal::CACHE_ONLY = 1;
+$template = new Petal (file => $file, base_dir => $data_dir);
+
+ok($template->cache_only, "\$Petal::CACHE_ONLY turns it on");
+
+
+# Confirm option can disable it
+
+$template = new Petal (file => $file, base_dir => $data_dir, cache_only => 0);
+
+ok(!$template->cache_only, "cache_only option turns it off again");
+
+
--- /dev/null
+++ t/097_cache_only.t
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+##############################################################################
+# Tests the 'language' option (and 'lang' alias) to Petal->new.
+# Uses t/data/language/*
+#
+
+# this is identical to t/052_Opt_language but with DISK_CACHE=1
+
+use Test::More 'no_plan';
+
+use warnings;
+use lib 'lib';
+
+use Petal;
+
+$Petal::MEMORY_CACHE = 1;
+$Petal::DISK_CACHE = 0;
+$Petal::CACHE_ONLY = 1;
+
+my $data_dir = 't/data';
+
+my $file     = 'cookbook.html';
+my $template = new Petal (file => $file, base_dir => $data_dir);
+ok (eval {$template->process()}, 'process() CACHE_ONLY=1 without args should succeed');
+
+$Petal::CACHE_ONLY = 0;
+$Petal::MEMORY_CACHE = 0;
+
+$file          = 'children.xml';
+my $template_A = new Petal (file => $file, base_dir => $data_dir);
+ok (!eval {$template_A->process()}, 'process() without args should fail');
+
+$Petal::MEMORY_CACHE = 1;
+
+$file          = 'children.xml';
+my $template_B = new Petal (file => $file, base_dir => $data_dir, cache_only => 1);
+ok (eval {$template_B->process()}, 'process() cache_only=1 without args should succeed');
+


More information about the MKDoc-commit mailing list