aboutsummaryrefslogtreecommitdiff
path: root/lib/TgLib/Cache.pm
blob: d3723b76d87473879a006ac71161becefad2353c (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
package TgLib::Cache;

use Storable qw<store retrieve>;
use File::Basename qw<dirname>;

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 = shift;

    # Fetch cache
    return bless((-f $CACHE_FILE
                  ? retrieve($CACHE_FILE)
                  : {'version' => $main::VERSION}), $class);
}

sub offset {
    my $cache = shift;
    $cache->{'offset'} = shift if @_;
    return $cache->{'offset'};
}

sub save {
    my $cache = shift;
    mkdir dirname $CACHE_FILE;
    store($cache, $CACHE_FILE);
}

1;