[BACK]Return to Cached.pm CVS log [TXT][DIR] Up to [local] / openbsd / OpenBSDTorrents / lib / BT / MetaInfo

Diff for /openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm between version 1.3 and 1.6

version 1.3, 2005/05/02 22:46:48 version 1.6, 2005/05/05 21:10:50
Line 1 
Line 1 
 # $Id$  # $Id$
 use strict;  use strict;
   
 package BT::OBTMetaInfo;  package BT::MetaInfo::Cached;
   
 require 5.6.0;  require 5.6.0;
 use vars qw( $VERSION @ISA );  use vars qw( $VERSION @ISA );
   
 use Digest::SHA1 qw(sha1);  use Cache::FileCache;
 use Fcntl ':flock'; # import LOCK_* constants  use File::Basename;
   
 use BT::MetaInfo;  use BT::MetaInfo;
 use base 'BT::MetaInfo';  use base 'BT::MetaInfo';
   
 use OpenBSDTorrents;  
   
 use Data::Dumper;  
   
 $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 $classname   = shift;          my $class = shift;
         return $classname->SUPER::new(@_);          my $file  = shift;
 }          my $cache_settings = shift;
   
           $cache_settings->{namespace} ||= 'BT::MetaInfo::Cached';
   
 sub info_hash_cached          my $cache = new Cache::FileCache( $cache_settings );
 {  
         my $self = shift;  
         my $torrent = shift;  
   
         return $self->SUPER::info_hash unless $torrent;          my $obj = (defined($file)) ? _load($file, $cache) : {};
   
         my $meta_file = $torrent;          $obj->{cache} = $cache;
         $meta_file =~ s/\.torrent$/.$OBT->{META_EXT}/;  
   
         my $hash = undef;          return(bless($obj, $class));
   }
   
         if (-e $meta_file) {  sub _load {
                 #print "Reading meta file: $meta_file\n";          my $file = shift;
                 open my $meta, $meta_file or die "Couldn't open $meta_file: $!";          my $cache = shift;
                 flock($meta, LOCK_SH);  
                 binmode $meta;  
   
                 $hash = do { local $/; <$meta> };          my $basename = basename($file);
   
           my $info = $cache->get( $basename );
   
                 flock($meta, LOCK_UN);          unless (defined $info) {
                 close $meta;                  $info = BT::MetaInfo::_load($file);
         } else {                  $cache->set( $basename, $info );
                 $hash = $self->SUPER::info_hash;          }
                 #print "Writing meta file: $meta_file\n";          return $info;
                 open my $meta, '>', $meta_file  }
                         or die "Couldn't open $meta_file: $!";  
                 flock($meta, LOCK_EX);  
                 binmode $meta;  
   
                 print $meta $hash;  sub _load {
           my $file = shift;
           my $cache = shift;
   
                 flock($meta, LOCK_UN);          my $basename = basename($file);
                 close $meta;  
           my $info = $cache->get( $basename );
   
           unless (defined $info) {
                   $info = BT::MetaInfo::_load($file);
                   $cache->set( $basename, $info );
         }          }
         #my $text_hash = unpack("H*", $hash);          return $info;
         #print "INFO_HASH: $text_hash\n";  
   
         return $hash;  
 }  }
   
 1  
   sub save
   {
           my ($self, $file) = @_;
           my $basename = basename($file);
   
           $self->SUPER::save($file, @_);
   
           my %info_hash = %$self; # unbless
           $self->cache->set->($basename, \%info_hash)
   }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.6

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>