5 |
|
# |
6 |
|
# Description |
7 |
|
# ----------- |
8 |
< |
# Simple url->file lookup |
8 |
> |
# Simple url->file lookup (persistent) |
9 |
|
# |
10 |
|
# Interface |
11 |
|
# --------- |
12 |
< |
# new(cachedir) : A new URLcache object |
12 |
> |
# new(cachedir) : A new URLcache object in a given directory |
13 |
|
# store(url,file) : store a url/file combination |
14 |
< |
# file(url) : retunr the file for a given url or "" if not there |
14 |
> |
# file(url) : return the file for a given url |
15 |
|
# delete(url) : remove from cache url |
16 |
|
# clear() : clear cache |
17 |
+ |
# filestore() : Return the directory to download files to |
18 |
+ |
# filename(url) : return a unique file/dir in cache for the given url |
19 |
|
|
20 |
|
package URL::URLcache; |
21 |
|
require 5.004; |
29 |
|
return $self; |
30 |
|
} |
31 |
|
|
32 |
+ |
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 |
|
sub init { |
56 |
|
my $self=shift; |
57 |
< |
$self->{cachedir}=shift; |
57 |
> |
$self->{cachetop}=shift; |
58 |
> |
$self->{cachedir}=$self->{cachetop}."/files"; |
59 |
> |
AddDir::adddir($self->{cachedir}); |
60 |
|
|
61 |
< |
$self->{cacheindex}=$self->{cachedir}."/index.db"; |
61 |
> |
$self->{cacheindex}=$self->{cachetop}."/index.db"; |
62 |
|
$self->{urlDB}=Utilities::HashDB->new(); |
63 |
|
if ( -f $self->{cacheindex} ) { |
64 |
|
$self->{urlDB}->restore($self->{cacheindex}); |
72 |
|
my $self=shift; |
73 |
|
my $url=shift; |
74 |
|
|
75 |
< |
my $found; |
76 |
< |
($found)=$self->{urlDB}->getdata($url); |
50 |
< |
return ( ($found eq "")?"":$found); |
75 |
> |
my @found=$self->{urlDB}->getdata($url); |
76 |
> |
return ( ($#found == -1)?"":$found[0]); |
77 |
|
} |
78 |
|
|
79 |
|
sub delete { |
80 |
|
my $self=shift; |
81 |
|
my $url=shift; |
82 |
+ |
unlink ($self->{urlDB}->getdata($URL)); |
83 |
|
$self->{urlDB}->deletedata($url); |
84 |
|
$self->{urlDB}->store($self->{cacheindex}); |
85 |
|
} |
96 |
|
|
97 |
|
sub clear { |
98 |
|
my $self=shift; |
99 |
< |
unlink $self->{cacheindex}; |
99 |
> |
foreach $item ( $self->{urlDB}->match() ) { |
100 |
> |
$self->delete($item->keys()); |
101 |
> |
} |
102 |
|
} |