From 49bfce9d5386b4c3b36b93b72475b0766dd83307 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 22 Aug 2015 22:02:52 +0300 Subject: [PATCH] Add caching to naodash_user --- lib/NetHack/NAOdash.pm | 39 +++++++++++++++++++++++++++++++++++++-- naodash | 15 +++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/NetHack/NAOdash.pm b/lib/NetHack/NAOdash.pm index af574ef..92f17ef 100644 --- a/lib/NetHack/NAOdash.pm +++ b/lib/NetHack/NAOdash.pm @@ -10,6 +10,8 @@ our $VERSION = '0.001'; our @EXPORT_OK = qw/naodash_xlog naodash_user/; our @EXPORT = @EXPORT_OK; +use File::Slurp; +use File::Spec::Functions qw/tmpdir catdir catfile/; use HTTP::Tiny; use List::Util qw/max min sum/; use List::MoreUtils qw/uniq/; @@ -111,11 +113,26 @@ sub naodash_xlog { ## no critic (RequireArgUnpacking) my $ht = HTTP::Tiny->new(agent => "NetHack-NAOdash/$VERSION "); -sub naodash_user { +sub _get_xlog_from_server { my ($name) = @_; my $ret = $ht->get("http://alt.org/nethack/player-all-xlog.php?player=$name"); die 'Error while retrieving xlogfile from alt.org: ' . $ret->{status} . ' ' . $ret->{reason} . "\n" unless $ret->{success}; - my ($xlog) = $ret->{content} =~ m{
(.*)
}i; + $ret->{content} =~ m{
(.*)
}i; +} + +sub _get_xlog { + my ($name) = @_; + return _get_xlog_from_server $name if $ENV{NAODASH_CACHE} && lc $ENV{NAODASH_CACHE} eq 'none'; + my $dir = $ENV{NAODASH_CACHE} || catdir tmpdir, 'naodash'; + mkdir $dir or die "Cannot create cache directory: $!\n" unless -d $dir; + my $file = catfile $dir, $name; + write_file $file, _get_xlog_from_server $name unless -f $file && time - (stat $file)[9] < 86400; + scalar read_file $file +} + +sub naodash_user { + my ($name) = @_; + my $xlog = _get_xlog $name; die "No xlogfile found for user $name\n" unless defined $xlog; naodash_xlog $xlog; } @@ -264,6 +281,24 @@ Retrieves the xlogfile of a user from NAO and gives it to B. Dies if no xlogfile is found or if the server cannot be contacted. +This method caches the downloaded xlogfiles for one day in the +directory named by the NAODASH_CACHE environment variable. + +=back + +=head1 ENVIRONMENT + +=over + +=item NAODASH_CACHE + +Path to a directory that should be used to cache xlogfiles downloaded +from NAO, or the special value 'none' (case-insensitive) to disable +caching. + +By default a directory named 'naodash' in the default temporary +directory (C<< File::Spec->tmpdir >>) is used. + =back =head1 SEE ALSO diff --git a/naodash b/naodash index c3aec0c..52b175e 100755 --- a/naodash +++ b/naodash @@ -28,6 +28,21 @@ results of the analysis. A terminal with ANSI escape code support is required. Do not parse this command's output. For computer-readable output, please use the L library directly. +=head1 ENVIRONMENT + +=over + +=item NAODASH_CACHE + +Path to a directory that should be used to cache xlogfiles downloaded +from NAO, or the special value 'none' (case-insensitive) to disable +caching. + +By default a directory named 'naodash' in the default temporary +directory (C<< File::Spec->tmpdir >>) is used. + +=back + =head1 SEE ALSO L, L, L, L -- 2.39.2