line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::UserAgent::Role::Cache::Driver::File; |
2
|
4
|
|
|
4
|
|
28
|
use Mojo::Base -base; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
28
|
|
3
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
621
|
use Mojo::File; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
213
|
|
5
|
4
|
|
|
4
|
|
25
|
use Mojo::Util qw(md5_sum url_unescape); |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
267
|
|
6
|
|
|
|
|
|
|
|
7
|
4
|
|
50
|
4
|
|
23
|
use constant DEBUG => $ENV{MOJO_CLIENT_DEBUG} || $ENV{MOJO_UA_CACHE_DEBUG} || 0; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
417
|
|
8
|
4
|
|
100
|
4
|
|
29
|
use constant RENAME => $ENV{MOJO_UA_CACHE_RENAME} || 0; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
4054
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has root_dir => sub { $ENV{MOJO_USERAGENT_CACHE_DIR} || Mojo::File::tempdir('mojo-useragent-cache-XXXXX') }; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub get { |
13
|
43
|
|
|
43
|
1
|
109
|
my ($self, $key) = @_; |
14
|
43
|
|
|
|
|
104
|
my $file = $self->_path($key); |
15
|
43
|
|
|
|
|
1249
|
$self->_try_to_rename($file, @$key) if RENAME and !-e $file; |
16
|
43
|
|
|
|
|
241
|
my $exists = -e $file; |
17
|
43
|
|
|
|
|
1061
|
warn qq(-- Reading Mojo::UserAgent cache file $file\n) if DEBUG and $exists; |
18
|
43
|
100
|
|
|
|
603
|
return $exists ? $file->slurp : undef; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub remove { |
22
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
23
|
1
|
|
|
|
|
3
|
my $file = $self->_path(shift); |
24
|
1
|
50
|
50
|
|
|
30
|
unlink $file or die "unlink $file: $!" if -e $file; |
25
|
1
|
|
|
|
|
101
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub set { |
29
|
18
|
|
|
18
|
1
|
41
|
my $self = shift; |
30
|
18
|
|
|
|
|
46
|
my $file = $self->_path(shift); |
31
|
18
|
|
|
|
|
521
|
my $dir = Mojo::File->new($file->dirname); |
32
|
18
|
|
|
|
|
980
|
warn qq(-- Writing Mojo::UserAgent cache file $file\n) if DEBUG; |
33
|
18
|
100
|
|
|
|
59
|
$dir->make_path({mode => 0755}) unless -d $dir; |
34
|
18
|
|
|
|
|
2778
|
$file->spurt(shift); |
35
|
18
|
|
|
|
|
2204
|
return $self; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _path { |
39
|
62
|
|
|
62
|
|
118
|
my ($self, @key) = ($_[0], @{$_[1]}); |
|
62
|
|
|
|
|
194
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $safe = sub { |
42
|
242
|
|
|
242
|
|
441
|
my $len = length; |
43
|
242
|
100
|
100
|
|
|
2026
|
($len < 100 && $len != 32 && m!^[\w+\.-]+$!) ? $_ : md5_sum($_); |
44
|
62
|
|
|
|
|
218
|
}; |
45
|
|
|
|
|
|
|
|
46
|
62
|
|
|
|
|
176
|
my $last = $safe->(local $_ = pop @key); |
47
|
62
|
|
|
|
|
197
|
return Mojo::File->new($self->root_dir, (map { $safe->() } @key), "$last.http"); |
|
180
|
|
|
|
|
1483
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Will be removed in the future |
51
|
|
|
|
|
|
|
sub _try_to_rename { |
52
|
1
|
|
|
1
|
|
32
|
my ($self, $to, @key) = @_; |
53
|
1
|
|
|
|
|
4
|
my @old = (shift @key, shift @key); # method and host |
54
|
1
|
50
|
|
|
|
3
|
my $body = $key[-1] =~ s!^\?b=!! ? pop @key : undef; |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
6
|
my $url = Mojo::URL->new('/'); |
57
|
1
|
50
|
|
|
|
61
|
$url->query->parse($1) if $key[-1] =~ m!^\?q=(.*)!; |
58
|
1
|
|
|
|
|
22
|
pop @key; |
59
|
1
|
|
|
|
|
4
|
$url->path->parts([map { url_unescape $_ } @key]); |
|
1
|
|
|
|
|
11
|
|
60
|
1
|
|
|
|
|
68
|
push @old, $url->path_query; |
61
|
|
|
|
|
|
|
|
62
|
1
|
50
|
|
|
|
350
|
push @old, $body if defined $body; |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
|
|
7
|
my $last = substr md5_sum(pop @old), 0, 12; |
65
|
1
|
|
|
|
|
5
|
my $from = Mojo::File->new($self->root_dir, shift @old, (map { substr md5_sum($_), 0, 12 } @old), "$last.http"); |
|
1
|
|
|
|
|
11
|
|
66
|
1
|
|
|
|
|
26
|
my $to_dir = Mojo::File->new($to->dirname); |
67
|
|
|
|
|
|
|
|
68
|
1
|
50
|
|
|
|
67
|
$to_dir->make_path({mode => 0755}) unless -d $to_dir; |
69
|
1
|
50
|
50
|
|
|
270
|
rename $from, $to or die "Rename $from $to: $!" if -e $from; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=encoding utf8 |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 NAME |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Mojo::UserAgent::Role::Cache::Driver::File - Default cache driver for Mojo::UserAgent::Role::Cache |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 SYNOPSIS |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $driver = Mojo::UserAgent::Role::Cache::Driver::File->new; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$driver->set(\@key, $data); |
85
|
|
|
|
|
|
|
$data = $driver->get(\@key); |
86
|
|
|
|
|
|
|
$driver->remove(\@key); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 DESCRIPTION |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
L is the default cache driver for |
91
|
|
|
|
|
|
|
L. It should provide the same interface as |
92
|
|
|
|
|
|
|
L. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 root_dir |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$str = $self->root_dir; |
99
|
|
|
|
|
|
|
$self = $self->root_dir("/path/to/mojo-useragent-cache"); |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Where to store the cached files. Defaults to the C |
102
|
|
|
|
|
|
|
environment variable or a L. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 METHODS |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 get |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$data = $self->get(\@key); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Retrive data from the cache. Returns C if the C<@key> is not L. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 remove |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$self = $self->remove(\@key); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Removes data from the cache, by C<@key>. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 set |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$self = $self->set(\@key => $data); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Stores new C<$data> in the cache. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 SEE ALSO |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
L. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |