line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::UserAgent::Role::Cache::Driver::File; |
2
|
2
|
|
|
2
|
|
14
|
use Mojo::Base -base; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
13
|
|
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
306
|
use Mojo::File; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
113
|
|
5
|
2
|
|
|
2
|
|
16
|
use Mojo::Util 'md5_sum'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
870
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has root_dir => sub { $ENV{MOJO_USERAGENT_CACHE_DIR} || Mojo::File::tempdir('mojo-useragent-cache-XXXXX') }; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub get { |
10
|
3
|
|
|
3
|
1
|
8
|
my $self = shift; |
11
|
3
|
|
|
|
|
9
|
my $file = $self->_path(shift); |
12
|
3
|
100
|
|
|
|
634
|
return -e $file ? $file->slurp : undef; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub remove { |
16
|
1
|
|
|
1
|
1
|
2022
|
my $self = shift; |
17
|
1
|
|
|
|
|
4
|
my $file = $self->_path(shift); |
18
|
1
|
50
|
50
|
|
|
30
|
unlink $file or die "unlink $file: $!" if -e $file; |
19
|
1
|
|
|
|
|
103
|
return $self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub set { |
23
|
2
|
|
|
2
|
1
|
5
|
my $self = shift; |
24
|
2
|
|
|
|
|
5
|
my $file = $self->_path(shift); |
25
|
2
|
|
|
|
|
62
|
my $dir = Mojo::File->new($file->dirname); |
26
|
2
|
50
|
|
|
|
128
|
$dir->make_path({mode => 0755}) unless -d $dir; |
27
|
2
|
|
|
|
|
395
|
$file->spurt(shift); |
28
|
2
|
|
|
|
|
264
|
return $self; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _path { |
32
|
6
|
|
|
6
|
|
21
|
my ($self, $key) = @_; |
33
|
6
|
|
|
|
|
10
|
my $method = shift @$key; |
34
|
6
|
|
|
|
|
36
|
my $last = substr md5_sum(pop @$key), 0, 12; |
35
|
|
|
|
|
|
|
|
36
|
6
|
|
|
|
|
24
|
return Mojo::File->new($self->root_dir, $method, (map { substr md5_sum($_), 0, 12 } @$key), "$last.http"); |
|
0
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
1; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=encoding utf8 |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 NAME |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Mojo::UserAgent::Role::Cache::Driver::File - Default cache driver for Mojo::UserAgent::Role::Cache |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 SYNOPSIS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $driver = Mojo::UserAgent::Role::Cache::Driver::File->new; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$driver->set($key, $data); |
52
|
|
|
|
|
|
|
$data = $driver->get($key); |
53
|
|
|
|
|
|
|
$driver->remove($key); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 DESCRIPTION |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
L is the default cache driver for |
58
|
|
|
|
|
|
|
L. It should provide the same interface as |
59
|
|
|
|
|
|
|
L. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 root_dir |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$str = $self->root_dir; |
66
|
|
|
|
|
|
|
$self = $self->root_dir("/path/to/mojo-useragent-cache"); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Where to store the cached files. Defaults to the C |
69
|
|
|
|
|
|
|
environment variable or a L. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 METHODS |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 get |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
$data = $self->get($key); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Retrive data from the cache. Returns C if the C<$key> is not L. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 remove |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
$self = $self->remove($key); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Removes data from the cache, by C<$key>. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 set |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
$self = $self->set($key => $data); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Stores new C<$data> in the cache. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 SEE ALSO |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
L. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |