summaryrefslogtreecommitdiffstats
path: root/lib/Crawl/Bot/Role/CachedItems.pm
blob: b2db856b312e7198c72cfae82feadacb243bbde7 (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
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
package Crawl::Bot::Role::CachedItems;
use Moose::Role;

use autodie;
use File::Spec;

requires 'data_dir', 'current_items', 'item_to_id';

has items => (
    traits  => ['Hash'],
    isa     => 'HashRef',
    builder => '_build_items',
    handles => {
        has_item  => 'exists',
        items     => 'keys',
        _add_item => 'set',
    },
);

sub _build_items {
    my $self = shift;
    warn "Loading item cache for " . blessed($self);
    my $file = $self->item_cache_file;
    my %items;
    if (-r $file) {
        open my $fh, '<', $file;
        while (<$fh>) {
            chomp;
            $items{$_} = 1;
        }
    }
    else {
        $self->each_current_item(sub {
            my $item = shift;
            my $id = $self->item_to_id($item);
            $items{$id} = 1;
        });
    }
    warn "Done loading";
    \%items;
}

sub add_item {
    my $self = shift;
    $self->_add_item($_[0], 1);
}

sub each_current_item {
    my $self = shift;
    my ($code) = @_;
    for my $item ($self->current_items) {
        $code->($item);
    }
}

sub item_cache_file {
    my $self = shift;
    my $class = blessed($self);
    (my $plugin = $class) =~ s/^Crawl::Bot::Plugin:://;
    $plugin = lc($plugin);
    my $file = "${plugin}_item_cache";
    return File::Spec->catfile($self->data_dir, $file);
}

sub save_item_cache {
    my $self = shift;
    open my $fh, '>', $self->item_cache_file;
    $fh->print("$_\n") for $self->items;
}

after BUILDALL => sub {
    my $self = shift;
    $self->save_item_cache;
};

no Moose::Role;

1;