App-Greple-xlate

 view release on metacpan or  search on metacpan

lib/App/Greple/xlate/Cache.pm  view on Meta::CPAN


my %default = (
    name => '',		# cache filename
    saved => undef,	# saved hash
    current => undef,	# current using hash
    clear => 0,		# clean up cache data
    accessed => {},	# accessed keys
    order => [],	# accessed keys in order
    accumulate => 0,	# do not delete unused entry
    force_update => 0,	# update cache file anyway
    updated => 0,	# number of updated entries
    format => 'list',	# saving cache file format
);

for my $key (keys %default) {
    no strict 'refs';
    *{$key} = sub :lvalue { $_[0]->{$key} }
}

sub new {
    my $class = shift;

lib/App/Greple/xlate/Cache.pm  view on Meta::CPAN

sub set {
    my $obj = shift;
    pairmap {
	if (ref $a eq 'ARRAY' and ref $b eq 'ARRAY') {
	    @$a == @$b or die;
	    $obj->set(mesh $a, $b);
	} else {
	    my $c = $obj->current->{$a} //= delete $obj->saved->{$a};
	    if (not defined $c or $c ne $b) {
		$obj->current->{$a} = $b;
		$obj->updated++;
	    }
	}
    } @_;
    $obj;
}

sub json {
    JSON->new->utf8->canonical->pretty;
}

lib/App/Greple/xlate/Cache.pm  view on Meta::CPAN

	warn "read cache from $file\n";
    } else {
	$obj->{saved} = {};
    }
    $obj;
}

sub update {
    my $obj = shift;
    my $file = $obj->name || return;
    if (not $obj->force_update and $obj->updated == 0) {
	if (%{$obj->saved} == 0) {
	    return;
	} elsif ($obj->accumulate) {
	    for (keys %{$obj->saved}) {
		$obj->current->{$_} //= delete $obj->saved->{$_};
	    }
	}
    }
    while (my($k, $v) = each %{$obj->current}) {
	delete $obj->current->{$k} if not defined $v;



( run in 0.243 second using v1.01-cache-2.11-cpan-05444aca049 )