1package FEAR::API::ChksumRepos; 2 3use strict; 4use Carp; 5use DB_File; 6use Spiffy -Base; 7use File::Temp qw/ :POSIX /; 8 9field 'db_file'; 10field 'pool' => {}; 11field 'dbx'; 12field 'remove_on_destroy'; 13 14sub new() { 15 my $self = bless {}, shift; 16 my %opt = @_; 17 $self->remove_on_destroy($opt{remove_on_destroy}); 18 $self->tie($opt{'file'}); 19 $self; 20} 21 22sub tie { 23 my $file = shift; 24 my $tmpfile = 25 $self->db_file( $file || File::Temp::tmpnam() || croak "Please specify one file" ); 26 $self->{'dbx'} = 27 tie %{$self->{'pool'}}, 'DB_File', $self->{'db_file'}, 28 O_RDWR | O_CREAT, 0666, $DB_BTREE or croak $!; 29} 30 31sub add { 32 my ($key, $value) = @_; 33 $self->{pool}{$key} = ($value || 1) if $key; 34} 35 36sub value { 37 my $key = shift; 38 return $self->{pool}{$key} if $key; 39} 40 41sub del { 42 my $key = shift; 43 delete $self->{pool}{$key} if $key; 44} 45 46sub sync { 47 $self->dbx->sync; 48} 49 50sub has { 51 my $key = shift; 52 return exists $self->{pool}{$key} if $key; 53} 54 55sub empty { 56 $self->pool({}); 57 $self->sync; 58} 59 60sub remove { 61 remove $self->db_file; 62} 63 64sub DESTROY { 65 unlink $self->db_file if $self->{remove_on_destroy}; 66} 67 681; 69