diff options
author | Guillermo Ramos | 2019-06-25 12:57:22 +0200 |
---|---|---|
committer | Guillermo Ramos | 2019-06-25 13:03:53 +0200 |
commit | 077112d7242df2d1a26d90506b23045c6a6eb4ab (patch) | |
tree | 9509e135485c123a9f797347d0448e36eb46b84a /lib | |
parent | 16cd42e6fbfbf6fff06fd50926ac374b771e3f97 (diff) | |
download | tgutils-077112d7242df2d1a26d90506b23045c6a6eb4ab.tar.gz |
TgLib::Cache does its own logging
Diffstat (limited to 'lib')
-rw-r--r-- | lib/TgLib/Cache.pm | 26 |
1 files changed, 17 insertions, 9 deletions
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<store retrieve>; use File::Basename qw<dirname>; use File::Path qw<make_path>; +use Data::Dumper; use TgLib::Env qw<$CACHE_HOME>; @@ -12,24 +13,31 @@ our @EXPORT = qw<new>; 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; |