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

1.7     ! andrew      1: # $Id: Cached.pm,v 1.6 2005/05/05 20:10:50 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.7     ! andrew     15: $VERSION = do { my @r = (q$Id: Cached.pm,v 1.6 2005/05/05 20:10:50 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: }
1.5       andrew     33:
1.4       andrew     34: sub _load {
                     35:        my $file = shift;
1.5       andrew     36:        my $cache = shift;
                     37:
                     38:        my $basename = basename($file);
1.4       andrew     39:
1.5       andrew     40:        my $info = $cache->get( $basename );
1.1       andrew     41:
1.5       andrew     42:        unless (defined $info) {
1.4       andrew     43:                $info = BT::MetaInfo::_load($file);
1.5       andrew     44:                $cache->set( $basename, $info );
1.4       andrew     45:        }
                     46:        return $info;
                     47: }
1.3       andrew     48:
1.1       andrew     49:
1.6       andrew     50: sub save
                     51: {
                     52:        my ($self, $file) = @_;
                     53:        my $basename = basename($file);
                     54:
                     55:        $self->SUPER::save($file, @_);
                     56:
                     57:         my %info_hash = %$self; # unbless
                     58:        $self->cache->set->($basename, \%info_hash)
                     59: }

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