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

1.6     ! andrew      1: # $Id: Cached.pm,v 1.5 2005/05/05 20:08:04 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.6     ! andrew     15: $VERSION = do { my @r = (q$Id: Cached.pm,v 1.5 2005/05/05 20:08:04 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:
                     27:         my $obj = (defined($file)) ? _load($file, $cache) : {};
                     28:
                     29:        $obj->{cache} = $cache;
                     30:
1.4       andrew     31:         return(bless($obj, $class));
1.1       andrew     32: }
                     33:
1.6     ! andrew     34: sub _load {
        !            35:        my $file = shift;
        !            36:        my $cache = shift;
        !            37:
1.5       andrew     38:        my $basename = basename($file);
1.6     ! andrew     39:
        !            40:        my $info = $cache->get( $basename );
1.5       andrew     41:
1.6     ! andrew     42:        unless (defined $info) {
        !            43:                $info = BT::MetaInfo::_load($file);
        !            44:                $cache->set( $basename, $info );
        !            45:        }
        !            46:        return $info;
1.5       andrew     47: }
                     48:
1.4       andrew     49: sub _load {
                     50:        my $file = shift;
1.5       andrew     51:        my $cache = shift;
                     52:
                     53:        my $basename = basename($file);
1.4       andrew     54:
1.5       andrew     55:        my $info = $cache->get( $basename );
1.1       andrew     56:
1.5       andrew     57:        unless (defined $info) {
1.4       andrew     58:                $info = BT::MetaInfo::_load($file);
1.5       andrew     59:                $cache->set( $basename, $info );
1.4       andrew     60:        }
                     61:        return $info;
                     62: }
1.3       andrew     63:
1.1       andrew     64:
1.6     ! andrew     65: sub save
        !            66: {
        !            67:        my ($self, $file) = @_;
        !            68:        my $basename = basename($file);
        !            69:
        !            70:        $self->SUPER::save($file, @_);
        !            71:
        !            72:         my %info_hash = %$self; # unbless
        !            73:        $self->cache->set->($basename, \%info_hash)
        !            74: }

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