diff options
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; |