1 |
williamc |
1.1 |
#
|
2 |
|
|
# URLcache.pm
|
3 |
|
|
#
|
4 |
|
|
# Originally Written by Christopher Williams
|
5 |
|
|
#
|
6 |
|
|
# Description
|
7 |
|
|
# -----------
|
8 |
williamc |
1.2 |
# Simple url->file lookup (persistent)
|
9 |
williamc |
1.1 |
#
|
10 |
|
|
# Interface
|
11 |
|
|
# ---------
|
12 |
williamc |
1.2 |
# new(cachedir) : A new URLcache object in a given directory
|
13 |
williamc |
1.1 |
# store(url,file) : store a url/file combination
|
14 |
williamc |
1.2 |
# file(url) : return the file for a given url
|
15 |
williamc |
1.1 |
# delete(url) : remove from cache url
|
16 |
|
|
# clear() : clear cache
|
17 |
williamc |
1.2 |
# filestore() : Return the directory to download files to
|
18 |
|
|
# filename(url) : return a unique file/dir in cache for the given url
|
19 |
williamc |
1.1 |
|
20 |
|
|
package URL::URLcache;
|
21 |
|
|
require 5.004;
|
22 |
|
|
use Utilities::HashDB;
|
23 |
|
|
|
24 |
|
|
sub new {
|
25 |
|
|
my $class=shift;
|
26 |
|
|
$self={};
|
27 |
|
|
bless $self, $class;
|
28 |
|
|
$self->init(@_);
|
29 |
|
|
return $self;
|
30 |
|
|
}
|
31 |
|
|
|
32 |
williamc |
1.2 |
sub filestore {
|
33 |
|
|
my $self=shift;
|
34 |
|
|
return $self->{cachedir};
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
sub filename {
|
38 |
|
|
my $self=shift;
|
39 |
|
|
my $url=shift;
|
40 |
|
|
|
41 |
|
|
my $filenumber;
|
42 |
|
|
|
43 |
|
|
my $file=$self->file($url);
|
44 |
|
|
if ( $file eq "" ) {
|
45 |
|
|
# need to generate a new filename - a random number will do
|
46 |
|
|
srand();
|
47 |
|
|
do {
|
48 |
|
|
$filenumber=int(rand 9999999)+1;
|
49 |
|
|
$file=$self->filestore()."/".$filenumber;
|
50 |
|
|
} until ( ! ( -e $file ) );
|
51 |
|
|
}
|
52 |
|
|
return $file;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
williamc |
1.1 |
sub init {
|
56 |
|
|
my $self=shift;
|
57 |
williamc |
1.2 |
$self->{cachetop}=shift;
|
58 |
|
|
$self->{cachedir}=$self->{cachetop}."/files";
|
59 |
|
|
AddDir::adddir($self->{cachedir});
|
60 |
williamc |
1.1 |
|
61 |
williamc |
1.2 |
$self->{cacheindex}=$self->{cachetop}."/index.db";
|
62 |
williamc |
1.1 |
$self->{urlDB}=Utilities::HashDB->new();
|
63 |
|
|
if ( -f $self->{cacheindex} ) {
|
64 |
|
|
$self->{urlDB}->restore($self->{cacheindex});
|
65 |
|
|
}
|
66 |
|
|
else {
|
67 |
|
|
AddDir::adddir($self->{cachedir});
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
sub file {
|
72 |
|
|
my $self=shift;
|
73 |
|
|
my $url=shift;
|
74 |
|
|
|
75 |
williamc |
1.2 |
my @found=$self->{urlDB}->getdata($url);
|
76 |
|
|
return ( ($#found == -1)?"":$found[0]);
|
77 |
williamc |
1.1 |
}
|
78 |
|
|
|
79 |
|
|
sub delete {
|
80 |
|
|
my $self=shift;
|
81 |
|
|
my $url=shift;
|
82 |
williamc |
1.2 |
unlink ($self->{urlDB}->getdata($URL));
|
83 |
williamc |
1.1 |
$self->{urlDB}->deletedata($url);
|
84 |
|
|
$self->{urlDB}->store($self->{cacheindex});
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
sub store {
|
88 |
|
|
my $self=shift;
|
89 |
|
|
my $url=shift;
|
90 |
|
|
my $file=shift;
|
91 |
|
|
|
92 |
|
|
$self->{urlDB}->deletedata($url);
|
93 |
|
|
$self->{urlDB}->setdata($file,$url);
|
94 |
|
|
$self->{urlDB}->store($self->{cacheindex});
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
sub clear {
|
98 |
|
|
my $self=shift;
|
99 |
williamc |
1.2 |
foreach $item ( $self->{urlDB}->match() ) {
|
100 |
|
|
$self->delete($item->keys());
|
101 |
|
|
}
|
102 |
williamc |
1.1 |
}
|