| 1 | package MYDan::Agent::FileCache; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Agent::FileCache | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Agent::FileCache; | ||
| 10 | |||
| 11 | my $fc = MYDan::Agent::FileCache->new(); | ||
| 12 | |||
| 13 | $fc->save( '/my/file' ); | ||
| 14 | $fc->save( '/my/file' => md5 ); | ||
| 15 | |||
| 16 | $fc->check( md5 ); | ||
| 17 | |||
| 18 | $fc->get( '/my/file' => md5 ); | ||
| 19 | |||
| 20 | $fc->clean(); | ||
| 21 | |||
| 22 | =cut | ||
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | |||
| 26 | use MYDan; | ||
| 27 | use Digest::MD5; | ||
| 28 | use MYDan::Util::FastMD5; | ||
| 29 | |||
| 30 | sub new | ||
| 31 | { | ||
| 32 | my ( $class, %self ) = @_; | ||
| 33 | $self{path} ||= "$MYDan::PATH/var/run/filecache"; | ||
| 34 | bless \%self, ref $class || $class; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub save | ||
| 38 | { | ||
| 39 | my ( $this, $file ) = @_; | ||
| 40 | |||
| 41 | my $path = $this->{path}; | ||
| 42 | return 0 unless -e $path && defined $file && -f $file; | ||
| 43 | |||
| 44 | $this->_clean(); | ||
| 45 | |||
| 46 | my $tempmd5 = Digest::MD5->new()->add( $file.time,$$ )->hexdigest(); | ||
| 47 | die "save fail: $!" if system "cp '$file' '$path/$tempmd5.tmp'"; | ||
| 48 | |||
| 49 | my $md5 = MYDan::Util::FastMD5->hexdigest( "$path/$tempmd5.tmp" ); | ||
| 50 | |||
| 51 | die "save fail: $!" if system "mv '$path/$tempmd5.tmp' '$path/$md5'"; | ||
| 52 | |||
| 53 | return $md5; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub fastsave | ||
| 57 | { | ||
| 58 | my ( $this, $file, $md5 ) = @_; | ||
| 59 | |||
| 60 | my $path = $this->{path}; | ||
| 61 | return 0 unless -e $path && defined $file && -f $file; | ||
| 62 | |||
| 63 | $this->_clean(); | ||
| 64 | |||
| 65 | my $tempmd5 = Digest::MD5->new()->add( $file.time,$$ )->hexdigest(); | ||
| 66 | |||
| 67 | my @dev = map{ (stat $_)[0] }( $file, $path ); | ||
| 68 | my $cp = $dev[0] eq $dev[1] ? 'ln' : 'cp'; | ||
| 69 | die "save fail: $!" if system "$cp '$file' '$path/$tempmd5.tmp'"; | ||
| 70 | |||
| 71 | $md5 = MYDan::Util::FastMD5->hexdigest( "$path/$tempmd5.tmp" ) unless $md5; | ||
| 72 | |||
| 73 | die "save fail: $!" if system "mv '$path/$tempmd5.tmp' '$path/$md5'"; | ||
| 74 | |||
| 75 | return $md5; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | sub check | ||
| 81 | { | ||
| 82 | my ( $this, $md5 ) = @_; | ||
| 83 | |||
| 84 | my $path = $this->{path}; | ||
| 85 | return undef unless -e $path && defined $md5; | ||
| 86 | |||
| 87 | return -e "$path/$md5" ? "$path/$md5" : undef; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub get | ||
| 91 | { | ||
| 92 | my ( $this, $file, $md5 ) = @_; | ||
| 93 | |||
| 94 | my $path = $this->{path}; | ||
| 95 | return 0 unless -e $path && defined $file && defined $md5 && -e "$path/$md5"; | ||
| 96 | |||
| 97 | die "get fail: $!" if system "cp '$path/$md5' '$file'"; | ||
| 98 | return 1; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _clean | ||
| 102 | { | ||
| 103 | my $this = shift; | ||
| 104 | |||
| 105 | return unless -e $this->{path}; | ||
| 106 | |||
| 107 | for my $f ( grep{ -f } glob "$this->{path}/*" ) | ||
| 108 | { | ||
| 109 | if( $f =~ m/\/[a-zA-Z0-9]{32}\.tmp$/ ) | ||
| 110 | { | ||
| 111 | my $t = ( stat $f )[9]; | ||
| 112 | unlink $f if $t && $t < time - 3600; | ||
| 113 | } | ||
| 114 | elsif( $f =~ m/\/[a-zA-Z0-9]{32}$/ ) | ||
| 115 | { | ||
| 116 | my $t = ( stat $f )[9]; | ||
| 117 | unlink $f if $t && $t < time - 604800; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | return; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub usable | ||
| 125 | { | ||
| 126 | my $this = shift; | ||
| 127 | return -e $this->{path} ? 1 : 0; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; |