=================================================================== RCS file: /cvs/openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm,v retrieving revision 1.3 retrieving revision 1.9 diff -u -r1.3 -r1.9 --- openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm 2005/05/02 22:46:48 1.3 +++ openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm 2005/05/06 19:30:25 1.9 @@ -1,70 +1,71 @@ -# $Id: Cached.pm,v 1.3 2005/05/02 21:46:48 andrew Exp $ +# $Id: Cached.pm,v 1.9 2005/05/06 18:30:25 andrew Exp $ use strict; -package BT::OBTMetaInfo; +package BT::MetaInfo::Cached; require 5.6.0; use vars qw( $VERSION @ISA ); -use Digest::SHA1 qw(sha1); -use Fcntl ':flock'; # import LOCK_* constants +use Cache::FileCache; +use File::Basename; use BT::MetaInfo; use base 'BT::MetaInfo'; -use OpenBSDTorrents; +$VERSION = do { my @r = (q$Id: Cached.pm,v 1.9 2005/05/06 18:30:25 andrew Exp $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; -use Data::Dumper; - -$VERSION = do { my @r = (q$Id: Cached.pm,v 1.3 2005/05/02 21:46:48 andrew Exp $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; - sub new { - my $classname = shift; - return $classname->SUPER::new(@_); -} + my $class = shift; + my $file = shift; + my $cache_settings = shift; + if (ref $file eq 'HASH') { + $cache_settings = $file; + $file = undef; + } -sub info_hash_cached -{ - my $self = shift; - my $torrent = shift; + $cache_settings->{namespace} ||= 'BT::MetaInfo::Cached'; - return $self->SUPER::info_hash unless $torrent; + my $cache = new Cache::FileCache( $cache_settings ); - my $meta_file = $torrent; - $meta_file =~ s/\.torrent$/.$OBT->{META_EXT}/; + my $obj = (defined($file)) ? _load($file, $cache) : {}; - my $hash = undef; + bless($obj, $class); - if (-e $meta_file) { - #print "Reading meta file: $meta_file\n"; - open my $meta, $meta_file or die "Couldn't open $meta_file: $!"; - flock($meta, LOCK_SH); - binmode $meta; + $obj->{cache} = $cache; - $hash = do { local $/; <$meta> }; + return $obj; +} - flock($meta, LOCK_UN); - close $meta; - } else { - $hash = $self->SUPER::info_hash; - #print "Writing meta file: $meta_file\n"; - open my $meta, '>', $meta_file - or die "Couldn't open $meta_file: $!"; - flock($meta, LOCK_EX); - binmode $meta; +sub _load { + my $file = shift; + my $cache = shift; - print $meta $hash; + my $basename = basename($file); + + my $info = $cache->get( $basename ); - flock($meta, LOCK_UN); - close $meta; - + unless (defined $info) { + $info = BT::MetaInfo::_load($file); + $cache->set( $basename, $info ); } - #my $text_hash = unpack("H*", $hash); - #print "INFO_HASH: $text_hash\n"; - - return $hash; + return $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 + $cache->set($basename, \%info_hash) + } + + $self->{cache} = $cache; +}