many changes (see Changes)
[spider.git] / perl / Route / User.pm
index e99def158bcedfa087028790ae74882160c4ed44..6c91d3e2225ab283946fed48aaf70f7e761303c2 100644 (file)
@@ -11,6 +11,8 @@ package Route::User;
 use DXDebug;
 use Route;
 use DXUtil;
+use DXJSON;
+use Time::HiRes qw(gettimeofday);
 
 use strict;
 
@@ -21,6 +23,8 @@ $filterdef = $Route::filterdef;
 %list = ();
 $max = 0;
 
+our $cachefn = localdata('route_user_cache');
+
 sub count
 {
        my $n = scalar(keys %list);
@@ -94,7 +98,52 @@ sub delparent
     return $self->_dellist('parent', @_);
 }
 
+sub TO_JSON { return { %{ shift() } }; }
+
+sub write_cache
+{
+       my $json = DXJSON->new;
+       $json->canonical(isdbg('routecache')||0);
+       
+       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 dbg("Route::User: ERROR writing $cachefn $!"), return;
+               print $fh $_ for (sort @s);
+               $fh->close;
+       } else {
+               dbg("Route::User::write_cache error '$@'");
+               return;
+       }
+       my $diff = _diffms($ta);
+       dbg("Route::User::write_cache time to write: $diff mS");
+}
+
+sub read_cache
+{
+       my $json = DXJSON->new;
+       $json->canonical(isdbg('routecache'));
+       
+       my $ta = [ gettimeofday ];
+       my $count;
+       
+       my $fh = IO::File->new("$cachefn") or dbg("Route::User: ERROR reading $cachefn $!"), return;
+       while (my $l = <$fh>) {
+               chomp $l;
+               my ($k, $v) = split /:/, $l, 2;
+               $list{$k} = bless $json->decode($v) or dbg("Route::User: Error json error $! decoding '$v'"), next;
+               ++$count;
+       }
+       $fh->close if $fh;
 
+       my $diff = _diffms($ta);
+       dbg("Route::User::read_cache time to read $count records from $cachefn : $diff mS");
+}
 
 #
 # generic AUTOLOAD for accessors