=================================================================== RCS file: /cvs/openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm,v retrieving revision 1.1 retrieving revision 1.4 diff -u -r1.1 -r1.4 --- openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm 2005/05/02 20:37:28 1.1 +++ openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm 2005/05/05 20:34:31 1.4 @@ -1,20 +1,81 @@ +# $Id: Cached.pm,v 1.4 2005/05/05 19:34:31 andrew Exp $ +use strict; + package BT::OBTMetaInfo; require 5.6.0; -use strict; +use vars qw( $VERSION @ISA ); +use Digest::SHA1 qw(sha1); +use YAML qw/ DumpFile LoadFile /; + use BT::MetaInfo; +use base 'BT::MetaInfo'; -$VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; +use OpenBSDTorrents; +$VERSION = do { my @r = (q$Id: Cached.pm,v 1.4 2005/05/05 19:34:31 andrew Exp $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; + sub new { - my $classname = shift; - my $pass = shift; + my $class = shift; + my $file = shift; - my $self = $classname->SUPER::new(@_); + my $obj = (defined($file)) ? _load($file, @_) : {}; + return(bless($obj, $class)); } +sub _load { + my $file = shift; + my $meta_file = shift; + my $regen = shift; + + my $info; + if ($meta_file && ! $regen && -e $meta_file) { + $info = LoadFile($meta_file); + } -sub info_hash { return(sha1(bencode($_[0]->info))); } + unless ($info) { + $info = BT::MetaInfo::_load($file); + DumpFile($meta_file, $info) if $meta_file; + } + 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