/usr/share/perl5/Code/TidyAll/CacheModel.pm is in libcode-tidyall-perl 0.55~dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | package Code::TidyAll::CacheModel;
use strict;
use warnings;
use Digest::SHA qw(sha1_hex);
use Path::Tiny ();
use Moo;
our $VERSION = '0.55';
# todo, type checking?
has 'base_sig' => ( is => 'ro', default => "" );
has 'cache_engine' => ( is => 'ro' );
has 'cache_key' => ( is => 'lazy', clearer => 1 );
has 'cache_value' => ( is => 'lazy', clearer => 1 );
has 'file_contents' => ( is => 'rw', lazy => 1, builder => 1, trigger => 1, clearer => 1 );
has 'full_path' => ( is => 'ro', required => 1 );
has 'is_cached' => ( is => 'rw', lazy => 1, builder => 1, clearer => 1 );
has 'path' => ( is => 'ro', required => 1 );
sub _build_file_contents {
my ($self) = @_;
return Path::Tiny::path( $self->full_path )->slurp;
}
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) = @_;
my $cache_engine = $self->cache_engine or return;
my $cached_value = $cache_engine->get( $self->cache_key );
return defined $cached_value && $cached_value eq $self->cache_value;
}
sub update {
my ($self) = @_;
my $cache_engine = $self->cache_engine or return;
$cache_engine->set( $self->cache_key, $self->cache_value );
$self->is_cached(1);
return;
}
sub remove {
my ($self) = @_;
my $cache_engine = $self->cache_engine or return;
$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.55
=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;
=head1 DESCRIPTION
A cache model for Code::TidyAll. Different subclasses can employ different
caching techniques.
The basic model implemented here is simple; It stores in the cache a hash key
of the file contents keyed by a hash key of the file's path.
=head2 Attributes
=over
=item full_path (required, ro)
The full path to the file on disk
=item path (required, ro)
The local path to the file (i.e. what the cache system will consider the
canonical name of the file)
=item cache_engine (optional, default undef, ro)
A C<Code::TidyAll::Cache> compatible instance, or, if no caching is required
undef.
=item base_sig (optional, default empty string, ro)
A base signature.
=item file_contents (optional, default loads file contents from disk, rw)
=item is_cached (optional, default computed, rw)
A flag indicating if this is cached. By default checks that the cache key and
cache value match the cache.
=back
=head2 Methods
=over
=item cache_key
The computed cache key for the file
=item cache_value
The computed cache value for the file
=item update
Updates the cache
=item remove
Attempts to remove the value from the cache
=back
=head1 SUPPORT
Bugs may be submitted through
L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
=head1 AUTHORS
=over 4
=item *
Jonathan Swartz <swartz@pobox.com>
=item *
Dave Rolsky <autarch@urth.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 - 2016 by Jonathan Swartz.
This is free software; you can redistribute it and/or modify it under the same
terms as the Perl 5 programming language system itself.
=cut
|