[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.1 and 1.6

version 1.1, 2005/05/02 20:37:28 version 1.6, 2005/05/05 21:10:50
Line 1 
Line 1 
 package BT::OBTMetaInfo;  # $Id$
   use strict;
   
   package BT::MetaInfo::Cached;
   
 require 5.6.0;  require 5.6.0;
 use strict;  use vars qw( $VERSION @ISA );
   
   use Cache::FileCache;
   use File::Basename;
   
 use BT::MetaInfo;  use BT::MetaInfo;
   use base 'BT::MetaInfo';
   
 $VERSION = do { my @r = (q$Revision$ =~ /\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;
         my $pass = shift;          my $file  = shift;
           my $cache_settings = shift;
   
         my $self        = $classname->SUPER::new(@_);          $cache_settings->{namespace} ||= 'BT::MetaInfo::Cached';
   
           my $cache = new Cache::FileCache( $cache_settings );
   
           my $obj = (defined($file)) ? _load($file, $cache) : {};
   
           $obj->{cache} = $cache;
   
           return(bless($obj, $class));
 }  }
   
   sub _load {
           my $file = shift;
           my $cache = shift;
   
 sub info_hash { return(sha1(bencode($_[0]->info))); }          my $basename = basename($file);
   
           my $info = $cache->get( $basename );
   
           unless (defined $info) {
                   $info = BT::MetaInfo::_load($file);
                   $cache->set( $basename, $info );
           }
           return $info;
   }
   
   sub _load {
           my $file = shift;
           my $cache = shift;
   
           my $basename = basename($file);
   
           my $info = $cache->get( $basename );
   
           unless (defined $info) {
                   $info = BT::MetaInfo::_load($file);
                   $cache->set( $basename, $info );
           }
           return $info;
   }
   
   
   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.1  
changed lines
  Added in v.1.6

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