--- lib/TWiki/Plugins/VarCachePlugin.pm~	2011-04-30 02:05:45.000000000 -0400
+++ lib/TWiki/Plugins/VarCachePlugin.pm	2011-05-20 18:17:37.000000000 -0400
@@ -21,18 +21,20 @@
 package TWiki::Plugins::VarCachePlugin;
 
 # =========================
 use vars qw(
         $web $topic $user $installWeb $VERSION $RELEASE $pluginName
-        $debug $paramMsg
+        $debug $NO_PREFS_IN_TOPIC
     );
 
-$VERSION = '$Rev: 18518 (2011-04-29) $';
+$VERSION = '$Rev: 18518 (2011-05-18) $';
 $RELEASE = '2010-05-01';
 
 $pluginName = 'VarCachePlugin';  # Name of this Plugin
 
+$NO_PREFS_IN_TOPIC = 1;
+
 # =========================
 sub initPlugin
 {
     ( $topic, $web, $user, $installWeb ) = @_;
 
@@ -57,11 +59,11 @@
 
     TWiki::Func::writeDebug( "- ${pluginName}::beforeCommonTagsHandler( $_[2].$_[1] )" ) if $debug;
 
     return unless( $_[0] =~ /%VARCACHE/ );
 
-    $_[0] =~ s/%VARCACHE{(.*?)}%/_handleVarCache( $_[2], $_[1], $1 )/ge;
+    $_[0] =~ s/%VARCACHE(?:{(.*?)})?%/_handleVarCache( $_[2], $_[1], $1 )/ge;
 
     # if "read cache", replace all text with marker, to be filled in afterCommonTagsHandler
     $_[0] =~ s/^.*(%--VARCACHE\:read\:.*?--%).*$/$1/os;
 }
 
@@ -72,19 +74,20 @@
 
     TWiki::Func::writeDebug( "- ${pluginName}::afterCommonTagsHandler( $_[2].$_[1] )" ) if $debug;
 
     return unless( $_[0] =~ /%--VARCACHE\:/ );
 
-    if( $_[0] =~ /%--VARCACHE\:([a-z]+)\:?(.*?)--%/ ) {
+    if( $_[0] =~ /%--VARCACHE\:([a-z]+)\:?(.*?)(?:\001(.*?)\001)?--%/ ) {
         my $save = ( $1 eq "save" );
         my $age = $2 || 0;
+	my $tag = $3;
         my $cacheFilename = _cacheFileName( $_[2], $_[1], $save );
 
         if( $save ) {
             # update cache
             TWiki::Func::saveFile( $cacheFilename, $_[0] );
-            $msg = _formatMsg( $_[2], $_[1] );
+            $msg = _formatMsg( $_[2], $_[1], $tag );
             $_[0] =~ s/%--VARCACHE\:.*?--%/$msg/go;
 
             # cache addToHEAD info
             # FIXME: Enhance the TWiki::Func API to get the head info cleanly
             $cacheFilename = _cacheFileName( $_[2], $_[1], 0, 1 );
@@ -95,15 +98,14 @@
                 $htmlHeader = join( "\n",
                   map { '<!--VARCACHE_HEAD_'.$_.'-->'.$session->{_HTMLHEADERS}{$_} }
                   keys %{$session->{_HTMLHEADERS}} );
             }
             TWiki::Func::saveFile( $cacheFilename, $htmlHeader );
-
-        } else {
+       } else {
             # read cache
             my $text = TWiki::Func::readFile( $cacheFilename );
-            $msg = _formatMsg( $_[2], $_[1] );
+            $msg = _formatMsg( $_[2], $_[1], $tag );
             $msg =~ s/\$age/_formatAge($age)/geo;
             $text =~ s/%--VARCACHE.*?--%/$msg/go;
             $_[0] = $text;
 
             # restore addToHEAD info from cache
@@ -119,13 +121,12 @@
 }
 
 # =========================
 sub _formatMsg
 {
-    my ( $theWeb, $theTopic ) = @_;
+    my ( $theWeb, $theTopic, $msg ) = @_;
 
-    my $msg = $paramMsg; # FIXME: Global variable not reliable in mod_perl
     $msg =~ s|<nop>||go;
     $msg =~ s|\$link|%SCRIPTURL{view}%/%WEB%/%TOPIC%?varcache=refresh|go;
     $msg =~ s|%ATTACHURL%|%PUBURL%/$installWeb/$pluginName|go;
     $msg =~ s|%ATTACHURLPATH%|%PUBURLPATH%/$installWeb/$pluginName|go;
     $msg = TWiki::Func::expandCommonVariables( $msg, $theTopic, $theWeb );
@@ -166,46 +167,46 @@
     if( $query ) {
         my $tmp = $query->param( 'varcache' ) || "";
         if( $tmp eq "refresh" ) {
             $action = "refresh";
         } else {
-            $action = "" if( grep{ !/^refresh$/ } $query->param );
+            $action = "" if( grep{ !/^varcache$/ } $query->param );
         }
     }
-
     if( $action eq "check" ) {
         my $filename = _cacheFileName( $theWeb, $theTopic, 0 );
         if( -e $filename ) {
             my $now = time();
             my $cacheTime = (stat $filename)[9] || 10000000000;
             # CODE_SMELL: Assume file system for topics
             $filename = TWiki::Func::getDataDir() . "/$theWeb/$theTopic.txt";
             my $topicTime = (stat $filename)[9] || 10000000000;
             my $refresh = TWiki::Func::extractNameValuePair( $theArgs )
-                       || TWiki::Func::extractNameValuePair( $theArgs, "refresh" )
-                       || TWiki::Func::getPreferencesValue( "\U$pluginName\E_REFRESH" ) || 24;
+                       || TWiki::Func::extractNameValuePair( $theArgs, "refresh" );
+            $refresh = TWiki::Func::getPreferencesValue( "\U$pluginName\E_REFRESH" ) unless( defined $refresh );
+	    $refresh = 24 unless( defined $refresh );
             $refresh *= 3600;
-            if( ( ( $refresh == 0 ) || ( $cacheTime >= $now - $refresh ) )
+	    if( ( ( $refresh == 0 ) || ( $cacheTime >= $now - $refresh ) )
              && ( $cacheTime >= $topicTime ) ) {
                 # add marker for afterCommonTagsHandler to read cached file
-                $paramMsg = TWiki::Func::extractNameValuePair( $theArgs, "cachemsg" )
-                         || TWiki::Func::getPreferencesValue( "\U$pluginName\E_CACHEMSG" )
-                         || 'This topic was cached $age ago ([[$link][refresh]])';
+                my $paramMsg = TWiki::Func::extractNameValuePair( $theArgs, "cachemsg" )
+		            || TWiki::Func::getPreferencesValue( "\U$pluginName\E_CACHEMSG" )
+                            || 'This topic was cached $age ago ([[$link][refresh]])';
                 $cacheTime = sprintf( "%1.6f", ( $now - $cacheTime ) / 3600 );
-                return "%--VARCACHE\:read:$cacheTime--%";
+                return "%--VARCACHE\:read:$cacheTime\001$paramMsg\001--%";
             }
         }
         $action = "refresh";
     }
 
     if( $action eq "refresh" ) {
         # add marker for afterCommonTagsHandler to refresh cache file
-        $paramMsg = TWiki::Func::extractNameValuePair( $theArgs, "updatemsg" )
-                 || TWiki::Func::getPreferencesValue( "\U$pluginName\E_UPDATEMSG" )
-                 || 'This topic is now cached ([[$link][refresh]])';
+        my $paramMsg = TWiki::Func::extractNameValuePair( $theArgs, "updatemsg" )
+                    || TWiki::Func::getPreferencesValue( "\U$pluginName\E_UPDATEMSG" )
+                    || 'This topic is now cached ([[$link][refresh]])';
         
-        return "%--VARCACHE\:save--%";
+        return "%--VARCACHE\:save\001$paramMsg\001--%";
     }
 
     # else normal uncached processing
     return "";
 }
@@ -213,24 +214,17 @@
 # =========================
 sub _cacheFileName
 {
     my ( $web, $topic, $mkDir, $isHead ) = @_;
 
-    # Create web directory "pub/$web" if needed
-    my $dir = TWiki::Func::getPubDir() . "/$web";
+    my $dir = TWiki::Func::getWorkArea($pluginName) . "/$web";
     if( ( $mkDir ) && ( ! -e "$dir" ) ) {
-        umask( 002 );
-        mkdir( $dir, 0775 );
+        my $sumsk = umask( 002 );
+        mkdir( $dir, $TWiki::cfg{RCS}{dirPermission} );
+	umask( $sumsk );
     }
-    # Create topic directory "pub/$web/$topic" if needed
-    $dir .= "/$topic";
-    if( ( $mkDir ) && ( ! -e "$dir" ) ) {
-        umask( 002 );
-        mkdir( $dir, 0775 );
-    }
-    my $fileName = '_cache.txt';
-    $fileName    = '_cache.head' if( $isHead );
-    return "$dir/_${pluginName}${fileName}";
+
+    return "$dir/${topic}_cache." . ($isHead? 'head' : 'txt');
 }
 
 # =========================
 1;
