blob: dbbf2523068baf6b2e31e4310c84d18db81ff17b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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>;
use parent qw<Exporter>;
our @EXPORT = qw<new>;
my $CACHE_FILE = "$CACHE_HOME/tgutils/cache";
sub new {
my ($class, $logger) = @_;
my $cache = -f $CACHE_FILE
? retrieve($CACHE_FILE)
: { version => 1 };
$logger->debug("Using cache: " . Dumper($cache));
# Fetch cache
return bless { cache => $cache, logger => $logger }, $class;
}
sub 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 $self = shift;
make_path dirname $CACHE_FILE;
store($self->{'cache'}, $CACHE_FILE);
$self->{'logger'}->debug(sprintf "Saved cache: %s\n", Dumper($self->{'cache'}));
}
1;
|