version 1.5, 2005/05/05 21:08:04 |
version 1.12, 2006/05/15 19:47:04 |
|
|
# $Id$ |
# $RedRiver: Cached.pm,v 1.11 2005/05/21 00:05:13 andrew Exp $ |
use strict; |
use strict; |
|
|
package BT::MetaInfo::Cached; |
package BT::MetaInfo::Cached; |
|
|
require 5.6.0; |
require 5.6.0; |
use vars qw( $VERSION @ISA ); |
use vars qw( $VERSION @ISA ); |
|
|
use YAML; |
|
|
|
#use Digest::SHA1 qw(sha1); |
|
#use YAML qw/ DumpFile LoadFile /; |
|
|
|
use Cache::FileCache; |
use Cache::FileCache; |
use File::Basename; |
use File::Basename; |
|
use Digest::MD5; |
|
|
use BT::MetaInfo; |
use BT::MetaInfo; |
use base 'BT::MetaInfo'; |
use base 'BT::MetaInfo'; |
|
|
#use OpenBSDTorrents; |
|
|
|
$VERSION = do { my @r = (q$Id$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
$VERSION = do { my @r = (q$Id$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
|
|
sub new |
sub new |
|
|
my $file = shift; |
my $file = shift; |
my $cache_settings = shift; |
my $cache_settings = shift; |
|
|
$cache_settings->{namespace} ||= 'BT::MetaInfo::Cached'; |
if (ref $file eq 'HASH') { |
|
$cache_settings = $file; |
|
$file = undef; |
|
} |
|
|
|
$cache_settings->{namespace} ||= 'BT::MetaInfo::Cached'; |
|
$cache_settings->{default_expires_in} ||= 7 * 24 * 60 * 60; |
|
$cache_settings->{auto_purge_interval} ||= 1 * 1 * 10 * 60; |
|
|
my $cache = new Cache::FileCache( $cache_settings ); |
my $cache = new Cache::FileCache( $cache_settings ); |
|
|
my $obj = (defined($file)) ? _load($file, $cache) : {}; |
my $obj = (defined($file)) ? _load($file, $cache) : {}; |
|
|
|
bless($obj, $class); |
|
|
$obj->{cache} = $cache; |
$obj->{cache} = $cache; |
|
|
return(bless($obj, $class)); |
return $obj; |
} |
|
|
|
sub save { |
|
my ($self, $file) = @_; |
|
my $basename = basename($file); |
|
|
|
$self->SUPER::save($file, @_); |
|
|
|
my %info_hash = %$self; # unbless |
|
$self->cache->set->($basename, \%info_hash) |
|
} |
} |
|
|
sub _load { |
sub _load { |
|
|
|
|
my $info = $cache->get( $basename ); |
my $info = $cache->get( $basename ); |
|
|
|
my $md5; |
|
if (defined $info && $info->{'md5'}) { |
|
my $old_md5 = delete $info->{'md5'}; |
|
my $cur_md5 = _MD5_file($file); |
|
if ($old_md5 ne $cur_md5) { |
|
$cache->remove( $basename ); |
|
$info = undef; |
|
} |
|
$md5 = $cur_md5; |
|
} |
|
|
unless (defined $info) { |
unless (defined $info) { |
$info = BT::MetaInfo::_load($file); |
$info = BT::MetaInfo::_load($file); |
|
$info->{'md5'} = $md5; |
$cache->set( $basename, $info ); |
$cache->set( $basename, $info ); |
|
delete $info->{'md5'}; |
} |
} |
|
|
return $info; |
return $info; |
} |
} |
|
|
#sub cached |
|
#{ |
|
# my $self = shift; |
|
# my $which_info = shift; |
|
# my $file = shift; |
|
# my @args = @_; |
|
# |
|
# if (@args) { |
|
# return $self->$which_info(@args), |
|
# } |
|
# |
|
# return undef unless $which_info; |
|
# return $self->$which_info unless $file; |
|
# |
|
# my $info = undef; |
|
# |
|
# if (-e $file) { |
|
# #print "Reading meta file: $file\n"; |
|
# $info = LoadFile($file); |
|
# } |
|
# |
|
# unless ($info->{$which_info}) { |
|
# my $cur_info = $self->$which_info; |
|
# |
|
# $info->{$which_info} = $cur_info; |
|
# DumpFile($file, $info); |
|
# } |
|
# |
|
# if (defined $info->{$which_info}) { |
|
# return $info->{$which_info}; |
|
# } else { |
|
# return $self->$which_info; |
|
# } |
|
#} |
|
|
|
1 |
sub save |
|
{ |
|
my $self = shift; |
|
my $file = shift; |
|
my $basename = basename($file); |
|
|
|
my $cache = delete $self->{'cache'}; |
|
|
|
if ( $self->SUPER::save($file, @_) ) { |
|
my %info_hash = %$self; # unbless |
|
|
|
$info_hash{'md5'} = _MD5_file($file); |
|
$cache->set($basename, \%info_hash) |
|
} |
|
|
|
$self->{'cache'} = $cache; |
|
|
|
return 1; |
|
} |
|
|
|
sub _MD5_file |
|
{ |
|
my $file = shift; |
|
|
|
my $ctx = Digest::MD5->new; |
|
open my $fh, $file or die "Couldn't open FILE '$file': $!"; |
|
binmode $fh; |
|
$ctx->addfile($fh); |
|
close $fh; |
|
|
|
return $ctx->hexdigest; |
|
} |