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

1.8     ! andrew      1: # $Id: Cached.pm,v 1.7 2005/05/05 20:29:13 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.8     ! andrew     15: $VERSION = do { my @r = (q$Id: Cached.pm,v 1.7 2005/05/05 20:29:13 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.4       andrew     22:
1.5       andrew     23:        $cache_settings->{namespace} ||= 'BT::MetaInfo::Cached';
                     24:
                     25:        my $cache = new Cache::FileCache( $cache_settings );
                     26:
1.8     ! andrew     27:        my $obj = (defined($file)) ? _load($file, $cache) : {};
1.5       andrew     28:
1.8     ! andrew     29:        bless($obj, $class);
        !            30:
1.5       andrew     31:        $obj->{cache} = $cache;
                     32:
1.8     ! andrew     33:         return $obj;
        !            34: }
1.5       andrew     35:
1.4       andrew     36: sub _load {
                     37:        my $file = shift;
1.5       andrew     38:        my $cache = shift;
                     39:
                     40:        my $basename = basename($file);
1.4       andrew     41:
1.5       andrew     42:        my $info = $cache->get( $basename );
1.1       andrew     43:
1.5       andrew     44:        unless (defined $info) {
1.4       andrew     45:                $info = BT::MetaInfo::_load($file);
1.5       andrew     46:                $cache->set( $basename, $info );
1.4       andrew     47:        }
                     48:        return $info;
                     49: }
1.3       andrew     50:
1.1       andrew     51:
1.6       andrew     52: sub save
                     53: {
1.8     ! andrew     54:        my $self = shift;
        !            55:        my $file = shift;
1.6       andrew     56:        my $basename = basename($file);
                     57:
1.8     ! andrew     58:        my $cache = delete $self->{cache};
1.6       andrew     59:
1.8     ! andrew     60:        if ( $self->SUPER::save($file, @_) ) {
        !            61:                my %info_hash = %$self; # unbless
        !            62:                $cache->set($basename, \%info_hash)
        !            63:        }
        !            64:
        !            65:        $self->{cache} = $cache;
1.6       andrew     66: }

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