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

Annotation of openbsd/OpenBSDTorrents/lib/BT/MetaInfo/Cached.pm, Revision 1.5

1.5     ! andrew      1: # $Id: OBTMetaInfo.pm,v 1.4 2005/05/05 19:34:31 andrew Exp andrew $
1.2       andrew      2: use strict;
                      3:
1.5     ! andrew      4: package BT::MetaInfo::Cached;
1.1       andrew      5:
                      6: require 5.6.0;
1.2       andrew      7: use vars qw( $VERSION @ISA );
1.1       andrew      8:
1.5     ! andrew      9: use YAML;
1.3       andrew     10:
1.5     ! andrew     11: #use Digest::SHA1 qw(sha1);
        !            12: #use YAML qw/ DumpFile LoadFile /;
        !            13:
        !            14: use Cache::FileCache;
        !            15: use File::Basename;
        !            16:
1.1       andrew     17: use BT::MetaInfo;
1.3       andrew     18: use base 'BT::MetaInfo';
1.1       andrew     19:
1.5     ! andrew     20: #use OpenBSDTorrents;
1.3       andrew     21:
1.5     ! andrew     22: $VERSION = do { my @r = (q$Id: OBTMetaInfo.pm,v 1.4 2005/05/05 19:34:31 andrew Exp andrew $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
1.1       andrew     23:
                     24: sub new
                     25: {
1.4       andrew     26:        my $class = shift;
                     27:         my $file  = shift;
1.5     ! andrew     28:        my $cache_settings = shift;
1.4       andrew     29:
1.5     ! andrew     30:        $cache_settings->{namespace} ||= 'BT::MetaInfo::Cached';
        !            31:
        !            32:        my $cache = new Cache::FileCache( $cache_settings );
        !            33:
        !            34:         my $obj = (defined($file)) ? _load($file, $cache) : {};
        !            35:
        !            36:        $obj->{cache} = $cache;
        !            37:
1.4       andrew     38:         return(bless($obj, $class));
1.1       andrew     39: }
                     40:
1.5     ! andrew     41: sub save {
        !            42:        my ($self, $file) = @_;
        !            43:        my $basename = basename($file);
        !            44:
        !            45:        $self->SUPER::save($file, @_);
        !            46:
        !            47:         my %info_hash = %$self; # unbless
        !            48:        $self->cache->set->($basename, \%info_hash)
        !            49: }
        !            50:
1.4       andrew     51: sub _load {
                     52:        my $file = shift;
1.5     ! andrew     53:        my $cache = shift;
        !            54:
        !            55:        my $basename = basename($file);
1.4       andrew     56:
1.5     ! andrew     57:        my $info = $cache->get( $basename );
1.1       andrew     58:
1.5     ! andrew     59:        unless (defined $info) {
1.4       andrew     60:                $info = BT::MetaInfo::_load($file);
1.5     ! andrew     61:                $cache->set( $basename, $info );
1.4       andrew     62:        }
                     63:        return $info;
                     64: }
1.3       andrew     65:
1.4       andrew     66: #sub cached
                     67: #{
                     68: #      my $self = shift;
                     69: #        my $which_info = shift;
                     70: #      my $file = shift;
                     71: #      my @args = @_;
                     72: #
                     73: #      if (@args) {
                     74: #              return $self->$which_info(@args),
                     75: #      }
                     76: #
                     77: #      return undef unless $which_info;
                     78: #      return $self->$which_info unless $file;
                     79: #
                     80: #      my $info = undef;
                     81: #
                     82: #      if (-e $file) {
                     83: #              #print "Reading meta file: $file\n";
                     84: #              $info = LoadFile($file);
                     85: #      }
                     86: #
                     87: #      unless ($info->{$which_info}) {
                     88: #              my $cur_info = $self->$which_info;
                     89: #
                     90: #              $info->{$which_info} = $cur_info;
                     91: #              DumpFile($file, $info);
                     92: #      }
                     93: #
                     94: #      if (defined $info->{$which_info}) {
                     95: #              return $info->{$which_info};
                     96: #      } else {
                     97: #              return $self->$which_info;
                     98: #      }
                     99: #}
1.1       andrew    100:
1.2       andrew    101: 1

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