Code-TidyAll
view release on metacpan or search on metacpan
lib/Code/TidyAll/CacheModel.pm view on Meta::CPAN
lazy => 1,
builder => 1,
trigger => 1,
clearer => 1,
);
has full_path => (
is => 'ro',
isa => t('Path'),
required => 1,
);
has is_cached => (
is => 'rw',
isa => t('Bool'),
lazy => 1,
builder => 1,
clearer => 1,
);
has path => (
is => 'ro',
isa => t('Path'),
required => 1,
);
sub _build_file_contents {
my ($self) = @_;
return Path::Tiny::path( $self->full_path )->slurp_raw;
}
sub _trigger_file_contents {
my $self = shift;
$self->clear_cache_key;
$self->clear_is_cached;
$self->clear_cache_value;
return;
}
sub _build_cache_key {
my ($self) = @_;
return 'sig/' . $self->path;
}
sub _build_cache_value {
my ($self) = @_;
# this stat isn't ideal, but it'll do
my $last_mod = ( stat( $self->full_path ) )[9];
return $self->_sig( [ $self->base_sig, $last_mod, $self->file_contents ] );
}
sub _build_is_cached {
my ($self) = @_;
return unless $self->_has_cache_engine;
my $cached_value = $self->cache_engine->get( $self->cache_key );
return defined $cached_value && $cached_value eq $self->cache_value;
}
sub update {
my ($self) = @_;
return unless $self->_has_cache_engine;
$self->cache_engine->set( $self->cache_key, $self->cache_value );
$self->is_cached(1);
return;
}
sub remove {
my ($self) = @_;
return unless $self->_has_cache_engine;
$self->cache_engine->remove( $self->cache_key );
return;
}
sub _sig {
my ( $self, $data ) = @_;
return sha1_hex( join( ',', @$data ) );
}
1;
# ABSTRACT: Caching model for Code::TidyAll
__END__
=pod
=encoding UTF-8
=head1 NAME
Code::TidyAll::CacheModel - Caching model for Code::TidyAll
=head1 VERSION
version 0.85
=head1 SYNOPSIS
my $cache_model = Cody::TidyAll::CacheModel->new(
cache_engine => Code::TidyAll::Cache->new(...),
path => '/path/to/file/to/cache',
);
# check cache
print 'Yes!' if $cache_model->is_cached;
# update cache
$cache_model->clear_file_contents;
$cache_model->update;
# update the cache when you know the file contents
$cache_model->file_contents($new_content);
$cache_model->update;
# force removal from cache
$cache_model->remove;
( run in 1.582 second using v1.01-cache-2.11-cpan-39bf76dae61 )