[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.9

1.9     ! andrew      1: # $Id: Cached.pm,v 1.8 2005/05/05 21:44:03 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 Cache::FileCache;
                     10: use File::Basename;
                     11:
1.1       andrew     12: use BT::MetaInfo;
1.3       andrew     13: use base 'BT::MetaInfo';
1.1       andrew     14:
1.9     ! andrew     15: $VERSION = do { my @r = (q$Id: Cached.pm,v 1.8 2005/05/05 21:44:03 andrew Exp andrew $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
1.3       andrew     16:
1.1       andrew     17: sub new
                     18: {
1.4       andrew     19:        my $class = shift;
                     20:         my $file  = shift;
1.5       andrew     21:        my $cache_settings = shift;
1.9     ! andrew     22:
        !            23:        if (ref $file eq 'HASH') {
        !            24:                $cache_settings = $file;
        !            25:                $file = undef;
        !            26:        }
1.4       andrew     27:
1.5       andrew     28:        $cache_settings->{namespace} ||= 'BT::MetaInfo::Cached';
                     29:
                     30:        my $cache = new Cache::FileCache( $cache_settings );
                     31:
1.8       andrew     32:        my $obj = (defined($file)) ? _load($file, $cache) : {};
1.5       andrew     33:
1.8       andrew     34:        bless($obj, $class);
                     35:
1.5       andrew     36:        $obj->{cache} = $cache;
                     37:
1.8       andrew     38:         return $obj;
                     39: }
1.5       andrew     40:
1.4       andrew     41: sub _load {
                     42:        my $file = shift;
1.5       andrew     43:        my $cache = shift;
                     44:
                     45:        my $basename = basename($file);
1.4       andrew     46:
1.5       andrew     47:        my $info = $cache->get( $basename );
1.1       andrew     48:
1.5       andrew     49:        unless (defined $info) {
1.4       andrew     50:                $info = BT::MetaInfo::_load($file);
1.5       andrew     51:                $cache->set( $basename, $info );
1.4       andrew     52:        }
                     53:        return $info;
                     54: }
1.3       andrew     55:
1.1       andrew     56:
1.6       andrew     57: sub save
                     58: {
1.8       andrew     59:        my $self = shift;
                     60:        my $file = shift;
1.6       andrew     61:        my $basename = basename($file);
                     62:
1.8       andrew     63:        my $cache = delete $self->{cache};
1.6       andrew     64:
1.8       andrew     65:        if ( $self->SUPER::save($file, @_) ) {
                     66:                my %info_hash = %$self; # unbless
                     67:                $cache->set($basename, \%info_hash)
                     68:        }
                     69:
                     70:        $self->{cache} = $cache;
1.6       andrew     71: }

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