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