final versions of Route caching functions
[spider.git] / perl / Route / Node.pm
index 76d987572e3e789f6beee90ba9a0ed0bfcddbcf3..4aa8be9ead142bdee079e45a11150d9f29a95d07 100644 (file)
@@ -12,6 +12,8 @@ use DXDebug;
 use Route;
 use Route::User;
 use DXUtil;
+use DXJSON;
+use Time::HiRes qw(gettimeofday);
 
 use strict;
 
@@ -38,6 +40,7 @@ $filterdef = $Route::filterdef;
 %list = ();
 $max = 0;
 $obscount = 3;
+our $cachefn = localdata('route_node_cache');
 
 sub count
 {
@@ -392,6 +395,39 @@ sub PC92C_dxchan
        return (%{$parent->{PC92C_dxchan}});
 }
 
+sub TO_JSON { return { %{ shift() } }; }
+
+sub write_cache
+{
+       my $json = DXJSON->new;
+       $json->canonical(isdbg('routecache'));
+
+       my $ta = [ gettimeofday ];
+       my @s;
+       eval {
+               while (my ($k, $v) = each  %list) {
+                   push @s, "$k:" . $json->encode($v) . "\n";
+           }
+       };
+       if (!$@ && @s) {
+               my $fh = IO::File->new(">$cachefn") or confess("writing $cachefn $!");
+               if (isdbg("routecache")) {
+                       $fh->print(sort @s);
+               }
+               else {
+                       $fh->print(@s);
+               }
+               $fh->close;
+       } else {
+               dbg("Route::Node:Write_cache error '$@'");
+               return;
+       }
+       $json->indent(0)->canonical(0);
+       my $diff = _diffms($ta);
+       dbg("Route::Node:WRITE_CACHE time to write: $diff mS");
+}
+
+
 sub DESTROY
 {
        my $self = shift;