« blib/lib/Class/MakeMethods/Standard/Inheritable.pm
↑ Summary
blib/lib/Class/MakeMethods/Composite/Hash.pm »
| 1 | package Class::MakeMethods::Utility::DiskCache; | ||
| 2 | |||
| 3 | $VERSION = 1.008; | ||
| 4 | |||
| 5 | @EXPORT_OK = qw( disk_cache ); | ||
| 6 | sub import { require Exporter and goto &Exporter::import } # lazy Exporter | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use Carp; | ||
| 10 | use File::Spec; | ||
| 11 | use File::Path; | ||
| 12 | |||
| 13 | ######################################################################## | ||
| 14 | |||
| 15 | use vars qw( $DiskCacheDir ); | ||
| 16 | |||
| 17 | my $IndexFile = "methods.ix"; # file also serves as timestamp | ||
| 18 | my $FileEnding = ".mm"; | ||
| 19 | |||
| 20 | sub import { | ||
| 21 | my $package = shift; | ||
| 22 | if ( scalar @_ ) { | ||
| 23 | $DiskCacheDir = shift; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | ######################################################################## | ||
| 28 | |||
| 29 | my %HaveCheckedFreshness; | ||
| 30 | |||
| 31 | # $result = disk_cache( $package, $file, $sub, @args ); | ||
| 32 | sub disk_cache { | ||
| 33 | my ( $full_funct, $args_string, $function, @args ) = @_; | ||
| 34 | |||
| 35 | unless ( $DiskCacheDir and -e $DiskCacheDir ) { | ||
| 36 | return &$function( @args ); | ||
| 37 | } | ||
| 38 | |||
| 39 | my ($package, $func_name) = ( $full_funct =~ /^(.+)::(\w+)$/ ); | ||
| 40 | |||
| 41 | my $pack_dir = File::Spec->catdir( $DiskCacheDir, split /::/, $package ); | ||
| 42 | if ( ! -e $pack_dir and -w $DiskCacheDir ) { | ||
| 43 | mkpath($pack_dir, 0, 07777); | ||
| 44 | } | ||
| 45 | |||
| 46 | unless ( defined $HaveCheckedFreshness{$package} ) { | ||
| 47 | |||
| 48 | my $idx = File::Spec->catfile( $pack_dir, $IndexFile ); | ||
| 49 | |||
| 50 | my $signature = dependency_signature($package); | ||
| 51 | |||
| 52 | if ( -e $idx and read_file( $idx ) eq $signature ) { | ||
| 53 | $HaveCheckedFreshness{$package} = 1; | ||
| 54 | } else { | ||
| 55 | if ( ! -w $pack_dir ) { | ||
| 56 | # The index is out of date, but not writable -- abandon it | ||
| 57 | $HaveCheckedFreshness{ $package } = 0; | ||
| 58 | } else { | ||
| 59 | rmtree($pack_dir, 0, 1); | ||
| 60 | mkpath($pack_dir, 0, 07777); | ||
| 61 | |||
| 62 | write_file( $idx, $signature ); | ||
| 63 | $HaveCheckedFreshness{$package} = 1; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | unless ( $HaveCheckedFreshness{$package} ) { | ||
| 69 | return &$function( @args ); | ||
| 70 | } | ||
| 71 | |||
| 72 | my $func_dir = File::Spec->catdir( $pack_dir, $func_name ); | ||
| 73 | |||
| 74 | if ( ! -e $func_dir and -w $pack_dir ) { | ||
| 75 | mkpath($func_dir, 0, 07777); | ||
| 76 | } | ||
| 77 | my $file = File::Spec->catfile( $func_dir, $args_string . $FileEnding ); | ||
| 78 | |||
| 79 | if ( -e $file ) { | ||
| 80 | return read_file( $file ); | ||
| 81 | } | ||
| 82 | |||
| 83 | my $value = ( &$function( @args ) ); | ||
| 84 | |||
| 85 | if ( -e $func_dir and -w $func_dir ) { | ||
| 86 | write_file( $file, $value ); | ||
| 87 | } else { | ||
| 88 | warn "Can't cache: $file\n"; | ||
| 89 | } | ||
| 90 | |||
| 91 | return $value; | ||
| 92 | } | ||
| 93 | |||
| 94 | ######################################################################## | ||
| 95 | |||
| 96 | sub dependency_signature { | ||
| 97 | my @sources = shift; | ||
| 98 | my @results; | ||
| 99 | no strict 'refs'; | ||
| 100 | while ( my $class = shift @sources ) { | ||
| 101 | push @sources, @{"$class\::ISA"}; | ||
| 102 | push @results, $class unless ( grep { $_ eq $class } @results ); | ||
| 103 | } | ||
| 104 | |||
| 105 | foreach ( @results ) { | ||
| 106 | s!::!/!g; | ||
| 107 | $_ .= '.pm'; | ||
| 108 | } | ||
| 109 | return join "\n", map { $_ . ' '. (stat($::INC{ $_ }))[9] } @results; | ||
| 110 | } | ||
| 111 | |||
| 112 | ######################################################################## | ||
| 113 | |||
| 114 | sub read_file { | ||
| 115 | my $file = shift; | ||
| 116 | # warn "Reading file: $file\n"; | ||
| 117 | local *FILE; | ||
| 118 | open FILE, "$file" or die "Can't open $file: $!"; | ||
| 119 | local $/ = undef; | ||
| 120 | return <FILE>; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub write_file { | ||
| 124 | my $file = shift; | ||
| 125 | # warn "Writing file: $file \n"; | ||
| 126 | local *FILE; | ||
| 127 | open FILE, ">$file" or die "Can't write to $file: $!"; | ||
| 128 | print FILE shift(); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub read_dir { | ||
| 132 | my $dir = shift; | ||
| 133 | local *DIR; | ||
| 134 | opendir(DIR, $dir); | ||
| 135 | readdir(DIR); | ||
| 136 | } | ||
| 137 | |||
| 138 | ######################################################################## | ||
| 139 | |||
| 140 | 1; | ||
| 141 | |||
| 142 | __END__ |