X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FRoute%2FUser.pm;h=3a6f4f327b030a628c9f9a0c8f6309f864de4566;hb=bb3889c1d4930e2ca96b47aea10ce5e803737043;hp=0cd170299f482243af45b801d767376d48d6bdda;hpb=b7512d7880245203710b640c2def9a102f069983;p=spider.git diff --git a/perl/Route/User.pm b/perl/Route/User.pm index 0cd17029..3a6f4f32 100644 --- a/perl/Route/User.pm +++ b/perl/Route/User.pm @@ -11,21 +11,20 @@ package Route::User; use DXDebug; use Route; use DXUtil; +use DXJSON; +use Time::HiRes qw(gettimeofday); use strict; use vars qw(%list %valid @ISA $max $filterdef); @ISA = qw(Route); -%valid = ( - parent => '0,Parent Calls,parray', - ip => '0,IP Address', -); - $filterdef = $Route::filterdef; %list = (); $max = 0; +our $cachefn = localdata('route_user_cache'); + sub count { my $n = scalar(keys %list); @@ -99,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')); + + 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 $!"); + 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 confess("reading $cachefn $!"); + while (my $l = <$fh>) { + chomp $l; + my ($k, $v) = split /:/, $l, 2; + $list{$k} = bless $json->decode($v) or confess("json error decoding '$v'"); + ++$count; + } + $fh->close; + + my $diff = _diffms($ta); + dbg("Route::User::read_cache time to read $count records from $cachefn : $diff mS"); +} # # generic AUTOLOAD for accessors