Quoting Sam Tregar <[EMAIL PROTECTED]>:

> On Wed, 3 Jul 2002, Cees Hek wrote:
> 
> > But this would not work if you have caching turned on.  It would only
> > work the first time you called it.  Any time you call that template
> > afterwards, you will always get the first translation you did.
> 
> That's true.  I missed that part of your patch.  Caching and filters do
> tend to interact badly.  Still, I'm not sure that justifies your patch on
> its own.  Another way to solve the problem would be to expose the cache
> key generation to programmer control with a "cache_key" option or
> somesuch.

Looks like my mail from last night didn't go through, so I'll try again

I'm open to anything that will solve my problem.  Exposing the cache_key
generation would be a step in the right direction for my needs.  I have stripped
down my initial patch to only consolodate the cache function into using a common
function to get the cache_key (_get_cache_key()).  This function checks to see
if the user passed a 'cache_key' as an option and if not, just returns the
'filepath'.

Is this more or less what you had in mind?

By moving the cache_key generation into a single function, it will also allow
users to override the _get_cache_key function if they plan to extend HTML::Template.

I still believe that adding support for filter args would be useful (as well as
simple).  It would eliminate the need to use closures with filters.  But I'll
discuss this one step at a time...

Cees

The patch follows:

*** Template.pm_orig        Thu Jul  4 07:22:18 2002
--- Template.pm        Thu Jul  4 09:07:00 2002
***************
*** 1173,1178 ****
--- 1173,1188 ----
  # Caching subroutines - they handle getting and validating cache
  # records from either the in-memory or shared caches.

+ # determine a unique key to use for caching
+ sub _get_cache_key {
+   my $self = shift;
+   my $options = $self->{options};
+
+   return unless exists($options->{filename});
+
+   return $options->{cache_key} || $options->{filepath} ||
$self->_find_file($options->{filename});
+ }
+
  # handles the normal in memory cache
  use vars qw( %CACHE );
  sub _fetch_from_cache {
***************
*** 1182,1198 ****
    # return if there's no cache entry for this filename
    return unless exists($options->{filename});
    my $filepath = $self->_find_file($options->{filename});
!   return unless (defined($filepath) and
!                  exists $CACHE{$filepath});
    
    $options->{filepath} = $filepath;

    # validate the cache
    my $mtime = $self->_mtime($filepath);
    if (defined $mtime) {
      # return if the mtime doesn't match the cache
!     if (defined($CACHE{$filepath}{mtime}) and
!         ($mtime != $CACHE{$filepath}{mtime})) {
        $options->{cache_debug} and
          print STDERR "CACHE MISS : $filepath : $mtime\n";
        return;
--- 1192,1211 ----
    # return if there's no cache entry for this filename
    return unless exists($options->{filename});
    my $filepath = $self->_find_file($options->{filename});
!   my $cachekey = $self->_get_cache_key();
!
!   return unless (defined($cachekey) and
!                  exists $CACHE{$cachekey});
    
    $options->{filepath} = $filepath;
+   $options->{cachekey} = $cachekey;
  
    # validate the cache
    my $mtime = $self->_mtime($filepath);  
    if (defined $mtime) {
      # return if the mtime doesn't match the cache
!     if (defined($CACHE{$cachekey}{mtime}) and
!         ($mtime != $CACHE{$cachekey}{mtime})) {
        $options->{cache_debug} and
          print STDERR "CACHE MISS : $filepath : $mtime\n";
        return;
***************
*** 1200,1212 ****
  
      # if the template has includes, check each included file's mtime
      # and return if different
!     if (exists($CACHE{$filepath}{included_mtimes})) {
!       foreach my $filename (keys %{$CACHE{$filepath}{included_mtimes}}) {
          next unless
!           defined($CACHE{$filepath}{included_mtimes}{$filename});
          
          my $included_mtime = (stat($filename))[9];
!         if ($included_mtime != $CACHE{$filepath}{included_mtimes}{$filename}) {
            $options->{cache_debug} and
              print STDERR "### HTML::Template Cache Debug ### CACHE MISS :
$filepath : INCLUDE $filename : $included_mtime\n";
            
--- 1213,1225 ----
  
      # if the template has includes, check each included file's mtime
      # and return if different
!     if (exists($CACHE{$cachekey}{included_mtimes})) {
!       foreach my $filename (keys %{$CACHE{$cachekey}{included_mtimes}}) {
          next unless
!           defined($CACHE{$cachekey}{included_mtimes}{$filename});
          
          my $included_mtime = (stat($filename))[9];
!         if ($included_mtime != $CACHE{$cachekey}{included_mtimes}{$filename}) {
            $options->{cache_debug} and
              print STDERR "### HTML::Template Cache Debug ### CACHE MISS :
$filepath : INCLUDE $filename : $included_mtime\n";
            
***************
*** 1220,1229 ****
    
    $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ###
CACHE HIT : $filepath\n";
        
!   $self->{param_map} = $CACHE{$filepath}{param_map};
!   $self->{parse_stack} = $CACHE{$filepath}{parse_stack};
!   exists($CACHE{$filepath}{included_mtimes}) and
!     $self->{included_mtimes} = $CACHE{$filepath}{included_mtimes};
  
    # clear out values from param_map from last run
    $self->_normalize_options();
--- 1233,1242 ----
    
    $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ###
CACHE HIT : $filepath\n";
        
!   $self->{param_map} = $CACHE{$cachekey}{param_map};
!   $self->{parse_stack} = $CACHE{$cachekey}{parse_stack};
!   exists($CACHE{$cachekey}{included_mtimes}) and
!     $self->{included_mtimes} = $CACHE{$cachekey}{included_mtimes};
  
    # clear out values from param_map from last run
    $self->_normalize_options();
***************
*** 1234,1239 ****
--- 1247,1253 ----
    my $self = shift;
    my $options = $self->{options};
  
+   my $cachekey = $options->{cachekey} || $self->_get_cache_key();
    my $filepath = $options->{filepath};
    if (not defined $filepath) {
      $filepath = $self->_find_file($options->{filename});
***************
*** 1245,1263 ****
    $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ###
CACHE LOAD : $filepath\n";
      
    $options->{blind_cache} or
!     $CACHE{$filepath}{mtime} = $self->_mtime($filepath);
!   $CACHE{$filepath}{param_map} = $self->{param_map};
!   $CACHE{$filepath}{parse_stack} = $self->{parse_stack};
    exists($self->{included_mtimes}) and
!     $CACHE{$filepath}{included_mtimes} = $self->{included_mtimes};
  }
  
  # generates MD5 from filepath to determine filename for cache file
  sub _get_cache_filename {
    my ($self, $filepath) = @_;
  
!   # hash the filename ...
!   my $hash = Digest::MD5::md5_hex($filepath);
    
    # ... and build a path out of it.  Using the first two charcters
    # gives us 255 buckets.  This means you can have 255,000 templates
--- 1259,1277 ----
    $options->{cache_debug} and print STDERR "### HTML::Template Cache Debug ###
CACHE LOAD : $filepath\n";
      
    $options->{blind_cache} or
!     $CACHE{$cachekey}{mtime} = $self->_mtime($filepath);
!   $CACHE{$cachekey}{param_map} = $self->{param_map};
!   $CACHE{$cachekey}{parse_stack} = $self->{parse_stack};
    exists($self->{included_mtimes}) and
!     $CACHE{$cachekey}{included_mtimes} = $self->{included_mtimes};
  }
  
  # generates MD5 from filepath to determine filename for cache file
  sub _get_cache_filename {
    my ($self, $filepath) = @_;
  
!   # hash the cache_key ...
!   my $hash = Digest::MD5::md5_hex($self->_get_cache_key());
    
    # ... and build a path out of it.  Using the first two charcters
    # gives us 255 buckets.  This means you can have 255,000 templates
***************
*** 1390,1398 ****
  
    my $filepath = $self->_find_file($options->{filename});
    return unless defined $filepath;
  
    # fetch from the shared cache.
!   $self->{record} = $self->{cache}{$filepath};
    
    ($self->{mtime},
     $self->{included_mtimes},
--- 1404,1413 ----
  
    my $filepath = $self->_find_file($options->{filename});
    return unless defined $filepath;
+   my $cachekey = $self->_get_cache_key();
  
    # fetch from the shared cache.
!   $self->{record} = $self->{cache}{$cachekey};
    
    ($self->{mtime},
     $self->{included_mtimes},
***************
*** 1410,1417 ****
  }
  
  sub _validate_shared_cache {
!   my ($self, $filename, $record) = @_;
    my $options = $self->{options};
  
    $options->{shared_cache_debug} and print STDERR "### HTML::Template Cache
Debug ### SHARED CACHE VALIDATE : $filename\n";
  
--- 1425,1433 ----
  }
  
  sub _validate_shared_cache {
!   my ($self, $cachekey, $record) = @_;
    my $options = $self->{options};
+   my $filename = $self->{filepath} || $self->_find_file($options->{filename});
  
    $options->{shared_cache_debug} and print STDERR "### HTML::Template Cache
Debug ### SHARED CACHE VALIDATE : $filename\n";


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to