Index: trunk/DNSDB.pm
===================================================================
--- trunk/DNSDB.pm	(revision 543)
+++ trunk/DNSDB.pm	(revision 559)
@@ -27,4 +27,5 @@
 use Net::DNS;
 use Crypt::PasswdMD5;
+use Digest::MD5 qw(md5_hex);
 use Net::SMTP;
 use NetAddr::IP 4.027 qw(:lower);
@@ -201,4 +202,5 @@
 		sessiondir	=> 'session/',
 		exportcache	=> 'cache/',
+		usecache	=> 1,	# do we bother using the cache above?
 
 		# Session params
@@ -208,5 +210,5 @@
 		log_failures	=> 1,	# log all evarthing by default
 		perpage		=> 15,
-		max_fcgi_requests => 100,	# reasonable default?
+		maxfcgi		=> 10,	# reasonable default?
 		force_refresh	=> 1,
 		lowercase	=> 0,	# mangle as little as possible by default
@@ -1337,4 +1339,6 @@
       $cfg->{perpage}		= $1 if /^perpage\s*=\s*(\d+)/i;
       $cfg->{exportcache}	= $1 if m{^exportcache\s*=\s*([a-z0-9/_.-]+)}i;
+      $cfg->{usecache}		= $1 if m{^usecache\s*=\s*([a-z01]+)}i;
+      $cfg->{force_refresh}	= $1 if /^force_refresh\s*=\s*([a-z01]+)/i;
       $cfg->{lowercase}		= $1 if /^lowercase\s*=\s*([a-z01]+)/i;
 # not supported in dns.cgi yet
@@ -1344,5 +1348,4 @@
       $cfg->{rpcmode}		= $1 if /^rpc_mode\s*=\s*(socket|HTTP|XMLRPC)\s*$/i;
       $cfg->{maxfcgi}		= $1 if /^max_fcgi_requests\s*=\s*(\d+)\s*$/i;
-      $cfg->{force_refresh}	= $1 if /^force_refresh\s*=\s*([a-z01]+)/i;
       if (my ($tmp) = /^rpc_iplist\s*=\s*(.+)/i) {
         my @ips = split /[,\s]+/, $tmp;
@@ -3731,5 +3734,5 @@
 
   # do simple validation first
-  return ('FAIL', "TTL must be numeric") unless $ttl =~ /^\d+$/;
+  return ('FAIL', "TTL must be numeric") unless $ttl =~ /^-?\d+$/;
 
   # Quick check on hostname parts.  There are enough variations to justify a sub now.
@@ -3895,5 +3898,5 @@
 
   # do simple validation first
-  return ('FAIL', "TTL must be numeric") unless $ttl =~ /^\d+$/;
+  return ('FAIL', "TTL must be numeric") unless $ttl =~ /^-?\d+$/;
 
   # Quick check on hostname parts.  There are enough variations to justify a sub now.
@@ -4949,4 +4952,5 @@
   my $dbh = $self->{dbh};
   my $datafile = shift;
+  my $zonefilehandle = $datafile;	# makes cache/no-cache a little simpler
 
 ##fixme: slurp up further options to specify particular zone(s) to export
@@ -5036,12 +5040,20 @@
     eval {
 
-      # only update the cache file if the zone has changed, or if the cache file has nothing in it.
-      if ($self->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
-        open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
+      # write fresh records if:
+      #  - we are not using the cache
+      #  - force_refresh is set
+      #  - the zone has changed
+      #  - the cache file does not exist
+      #  - the cache file is empty
+      if (!$self->{usecache} || $self->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
+        if ($self->{usecache}) {
+          open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
+          $zonefilehandle = *ZONECACHE;
+        }
 
         # need to fetch this separately since the rest of the records all (should) have real IPs in val
         $soasth->execute($revid);
         my (@zsoa) = $soasth->fetchrow_array();
-        _printrec_tiny(*ZONECACHE,'y',\%recflags,$revzone,
+        _printrec_tiny($zonefilehandle,'y',\%recflags,$revzone,
           $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],'');
 
@@ -5060,7 +5072,6 @@
 #	  }
 
-          _printrec_tiny(*ZONECACHE, 'y', \%recflags, $revzone,
-            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive)
-		if *ZONECACHE;
+          _printrec_tiny($zonefilehandle, 'y', \%recflags, $revzone,
+            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
 
           $recflags{$recid} = 1;
@@ -5068,10 +5079,11 @@
         } # while ($recsth)
 
-        close ZONECACHE; # force the file to be written
-
-	# catch obvious write errors that leave an empty temp file
-        if (-s $tmpcache) {
-          rename $tmpcache, $cachefile
-            or die "Error overwriting cache file $cachefile with temporary file: $!\n";
+        if ($self->{usecache}) {
+          close ZONECACHE; # force the file to be written
+	  # catch obvious write errors that leave an empty temp file
+          if (-s $tmpcache) {
+            rename $tmpcache, $cachefile
+              or die "Error overwriting cache file $cachefile with temporary file: $!\n";
+          }
         }
 
@@ -5088,8 +5100,12 @@
       $zonesth->execute($revid);
     }
-    # Always stream the cache (even if stale or obsolete due to errors creating the new cache)
-    open CACHE, "<$cachefile";
-    print $datafile $_ while <CACHE>;
-    close CACHE;
+
+    if ($self->{usecache}) {
+      # We've already made as sure as we can that a cached zone file is "good",
+      # although possibly stale/obsolete due to errors creating a new one.
+      open CACHE, "<$cachefile";
+      print $datafile $_ while <CACHE>;
+      close CACHE;
+    }
 
   } # while ($revsth)
@@ -5112,7 +5128,15 @@
     eval {
 
-      # only update the cache file if the zone has changed, or if the cache file has nothing in it.
-      if ($self->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
-        open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
+      # write fresh records if:
+      #  - we are not using the cache
+      #  - force_refresh is set
+      #  - the zone has changed
+      #  - the cache file does not exist
+      #  - the cache file is empty
+      if (!$self->{usecache} || $self->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {
+        if ($self->{usecache}) {
+          open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n";
+          $zonefilehandle = *ZONECACHE;
+        }
 
         $recsth->execute($domid);
@@ -5129,7 +5153,6 @@
 	  }
 
-	  _printrec_tiny(*ZONECACHE, 'n', \%recflags,
-		$dom, $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive)
-		if *ZONECACHE;
+	  _printrec_tiny($zonefilehandle, 'n', \%recflags,
+		$dom, $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
 
 	  $recflags{$recid} = 1;
@@ -5137,10 +5160,12 @@
         } # while ($recsth)
 
-        close ZONECACHE; # force the file to be written
-
-	# catch obvious write errors that leave an empty temp file
-        if (-s $tmpcache) {
-          rename $tmpcache, $cachefile
-            or die "Error overwriting cache file $cachefile with temporary file: $!\n";
+
+        if ($self->{usecache}) {
+          close ZONECACHE; # force the file to be written
+	  # catch obvious write errors that leave an empty temp file
+          if (-s $tmpcache) {
+            rename $tmpcache, $cachefile
+              or die "Error overwriting cache file $cachefile with temporary file: $!\n";
+          }
         }
 
@@ -5157,8 +5182,12 @@
       $zonesth->execute($domid);
     }
-    # Always stream the cache (even if stale or obsolete due to errors creating the new cache)
-    open CACHE, "<$cachefile";
-    print $datafile $_ while <CACHE>;
-    close CACHE;
+
+    if ($self->{usecache}) {
+      # We've already made as sure as we can that a cached zone file is "good",
+      # although possibly stale/obsolete due to errors creating a new one.
+      open CACHE, "<$cachefile";
+      print $datafile $_ while <CACHE>;
+      close CACHE;
+    }
 
   } # while ($domsth)
