summaryrefslogtreecommitdiffstats
path: root/lib/Devscripts/JSONCache.pm
blob: cf056c072fb6fd9c7d31081c489ea423226fff73 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package Devscripts::JSONCache;

use strict;
use JSON;
use Moo;

has file => (is => 'rw', required => 1);

has saved => (is => 'rw');

has _data => (is => 'rw');

sub save_sec {
    my ($self, $obj) = @_;
    my $tmp = umask;
    umask 0177;
    open(my $fh, '>', $self->file) or ($self->saved(1) and die $!);
    print $fh JSON::to_json($obj);
    close $fh;
    umask $tmp;
}

sub data {
    my ($self) = @_;
    return $self->_data if $self->_data;
    my $res;
    if (-r $self->file) {
        open(F, $self->file) or ($self->saved(1) and die $!);
        $res = JSON::from_json(join('', <F>) || "{}");
        close F;
    } else {
        $self->save_sec({});
        $self->saved(0);
    }
    return $self->_data($res);
}

sub TIEHASH {
    my $r = shift->new({
        file => shift,
        @_,
    });
    # build data
    $r->data;
    return $r;
}

sub FETCH {
    return $_[0]->data->{ $_[1] };
}

sub STORE {
    $_[0]->data->{ $_[1] } = $_[2];
}

sub DELETE {
    delete $_[0]->data->{ $_[1] };
}

sub CLEAR {
    $_[0]->save({});
}

sub EXISTS {
    return exists $_[0]->data->{ $_[1] };
}

sub FIRSTKEY {
    my ($k) = sort { $a cmp $b } keys %{ $_[0]->data };
    return $k;
}

sub NEXTKEY {
    my ($self, $last) = @_;
    my $i    = 0;
    my @keys = map {
        return $_ if ($i);
        $i++ if ($_ eq $last);
        return ()
      }
      sort {
        $a cmp $b
      } keys %{ $_[0]->data };
    return @keys ? $keys[0] : ();
}

sub SCALAR {
    return scalar %{ $_[0]->data };
}

sub save {
    return if ($_[0]->saved);
    eval { $_[0]->save_sec($_[0]->data); };
    $_[0]->saved(1);
}

*DESTROY = *UNTIE = *save;

1;