From 077112d7242df2d1a26d90506b23045c6a6eb4ab Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Tue, 25 Jun 2019 12:57:22 +0200 Subject: TgLib::Cache does its own logging --- lib/TgLib/Cache.pm | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'lib/TgLib/Cache.pm') diff --git a/lib/TgLib/Cache.pm b/lib/TgLib/Cache.pm index b6bb9e6..dbbf252 100644 --- a/lib/TgLib/Cache.pm +++ b/lib/TgLib/Cache.pm @@ -3,6 +3,7 @@ package TgLib::Cache; use Storable qw; use File::Basename qw; use File::Path qw; +use Data::Dumper; use TgLib::Env qw<$CACHE_HOME>; @@ -12,24 +13,31 @@ our @EXPORT = qw; my $CACHE_FILE = "$CACHE_HOME/tgutils/cache"; sub new { - my $class = shift; + my ($class, $logger) = @_; + my $cache = -f $CACHE_FILE + ? retrieve($CACHE_FILE) + : { version => 1 }; + $logger->debug("Using cache: " . Dumper($cache)); # Fetch cache - return bless((-f $CACHE_FILE - ? retrieve($CACHE_FILE) - : {'version' => $main::VERSION}), $class); + return bless { cache => $cache, logger => $logger }, $class; } sub offset { - my $cache = shift; - $cache->{'offset'} = shift if @_; - return $cache->{'offset'}; + my $self = shift; + if (@_) { + $self->{'cache'}{'offset'} = shift; + $self->save; + } + return $self->{'cache'}{'offset'}; } +# Not needed to be called explicitly; every cache modification calls this sub save { - my $cache = shift; + my $self = shift; make_path dirname $CACHE_FILE; - store($cache, $CACHE_FILE); + store($self->{'cache'}, $CACHE_FILE); + $self->{'logger'}->debug(sprintf "Saved cache: %s\n", Dumper($self->{'cache'})); } 1; -- cgit v1.2.3