line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Route::Cache; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SUKRIA'; |
3
|
|
|
|
|
|
|
#ABSTRACT: route caching mechanism for L |
4
|
|
|
|
|
|
|
$Dancer::Route::Cache::VERSION = '1.3521'; |
5
|
2
|
|
|
2
|
|
464
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
60
|
|
6
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
46
|
|
7
|
2
|
|
|
2
|
|
8
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
151
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
14
|
use base 'Dancer::Object'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
184
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
15
|
use Dancer::Config 'setting'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
102
|
|
12
|
2
|
|
|
2
|
|
14
|
use Dancer::Error; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
67
|
|
13
|
2
|
|
|
2
|
|
10
|
use Dancer::Exception qw(:all); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
1777
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Dancer::Route::Cache->attributes('size_limit', 'path_limit'); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# static |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# singleton for the current cache object |
20
|
|
|
|
|
|
|
my $_cache; |
21
|
|
|
|
|
|
|
|
22
|
133
|
|
|
133
|
0
|
990
|
sub get {$_cache} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub reset { |
25
|
2
|
|
|
2
|
0
|
13
|
$_cache = Dancer::Route::Cache->new(); |
26
|
2
|
50
|
|
|
|
11
|
$_cache->{size_limit} = setting('route_cache_size_limit') |
27
|
|
|
|
|
|
|
if defined setting('route_cache_size_limit'); |
28
|
2
|
50
|
|
|
|
6
|
$_cache->{path_limit} = setting('route_cache_path_limit') |
29
|
|
|
|
|
|
|
if defined setting('route_cache_path_limit'); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# instance |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub init { |
35
|
3
|
|
|
3
|
1
|
8
|
my ($self, %args) = @_; |
36
|
3
|
|
100
|
|
|
21
|
$self->build_size_limit($args{'size_limit'} || '10M'); |
37
|
3
|
|
100
|
|
|
17
|
$self->build_path_limit($args{'path_limit'} || 600); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub build_path_limit { |
41
|
3
|
|
|
3
|
0
|
8
|
my ($self, $limit) = @_; |
42
|
3
|
50
|
|
|
|
9
|
if ($limit) { |
43
|
3
|
|
|
|
|
5
|
$self->{'path_limit'} = $limit; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
10
|
return $self->{'path_limit'}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub build_size_limit { |
50
|
3
|
|
|
3
|
0
|
7
|
my ($self, $limit) = @_; |
51
|
3
|
50
|
|
|
|
13
|
if ($limit) { |
52
|
3
|
|
|
|
|
13
|
$self->{'size_limit'} = $self->parse_size($limit); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
7
|
return $self->{'size_limit'}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub parse_size { |
59
|
8
|
|
|
8
|
1
|
4776
|
my ($self, $size) = @_; |
60
|
|
|
|
|
|
|
|
61
|
8
|
50
|
|
|
|
73
|
if ($size =~ /^(\d+)(K|M|G)?$/i) { |
62
|
8
|
|
|
|
|
23
|
my $base = $1; |
63
|
8
|
100
|
|
|
|
25
|
if (my $ext = $2) { |
64
|
7
|
100
|
|
|
|
27
|
$ext eq 'K' and return $base * 1024**1; |
65
|
5
|
100
|
|
|
|
32
|
$ext eq 'M' and return $base * 1024**2; |
66
|
1
|
50
|
|
|
|
6
|
$ext eq 'G' and return $base * 1024**3; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
3
|
return $base; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub route_from_path { |
74
|
96
|
|
|
96
|
1
|
5952
|
my ($self, $method, $path, $app_name) = @_; |
75
|
|
|
|
|
|
|
|
76
|
96
|
50
|
33
|
|
|
339
|
$method && $path |
77
|
|
|
|
|
|
|
or raise core_route => "Missing method or path"; |
78
|
|
|
|
|
|
|
|
79
|
96
|
100
|
|
|
|
188
|
$app_name = 'main' unless defined $app_name; |
80
|
|
|
|
|
|
|
|
81
|
96
|
|
100
|
|
|
387
|
return $self->{'cache'}{$app_name}{$method}{$path} || undef; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub store_path { |
85
|
548
|
|
|
548
|
1
|
5720973
|
my ($self, $method, $path, $route, $app_name) = @_; |
86
|
|
|
|
|
|
|
|
87
|
548
|
50
|
33
|
|
|
2932
|
$method && $path && $route |
|
|
|
33
|
|
|
|
|
88
|
|
|
|
|
|
|
or raise core_route => "Missing method, path or route"; |
89
|
|
|
|
|
|
|
|
90
|
548
|
100
|
|
|
|
1275
|
$app_name = 'main' unless defined $app_name; |
91
|
|
|
|
|
|
|
|
92
|
548
|
|
|
|
|
61155
|
$self->{'cache'}{$app_name}{$method}{$path} = $route; |
93
|
|
|
|
|
|
|
|
94
|
548
|
|
|
|
|
896
|
push @{$self->{'cache_array'}}, [$method, $path, $app_name]; |
|
548
|
|
|
|
|
1977
|
|
95
|
|
|
|
|
|
|
|
96
|
548
|
50
|
|
|
|
1805
|
if (my $limit = $self->size_limit) { |
97
|
548
|
|
|
|
|
1159
|
while ($self->route_cache_size() > $limit) { |
98
|
500
|
|
|
|
|
762
|
my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}}; |
|
500
|
|
|
|
|
632
|
|
|
500
|
|
|
|
|
1132
|
|
99
|
500
|
|
|
|
|
4757
|
delete $self->{'cache'}{$app_name}{$method}{$path}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
548
|
100
|
|
|
|
1267
|
if (my $limit = $self->path_limit) { |
104
|
48
|
|
|
|
|
94
|
while ($self->route_cache_paths() > $limit) { |
105
|
16
|
|
|
|
|
25
|
my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}}; |
|
16
|
|
|
|
|
22
|
|
|
16
|
|
|
|
|
48
|
|
106
|
16
|
|
|
|
|
70
|
delete $self->{'cache'}{$app_name}{$method}{$path}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub route_cache_size { |
112
|
1049
|
|
|
1049
|
1
|
1505
|
my $self = shift; |
113
|
1049
|
|
|
|
|
1239
|
my %cache = %{$self->{'cache'}}; |
|
1049
|
|
|
|
|
2637
|
|
114
|
1049
|
|
|
|
|
1655
|
my $size = 0; |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
|
2
|
|
18
|
use bytes; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
16
|
|
117
|
|
|
|
|
|
|
|
118
|
1049
|
|
|
|
|
1948
|
foreach my $app_name (keys %cache) { |
119
|
1049
|
|
|
|
|
1419
|
$size += length $app_name; |
120
|
|
|
|
|
|
|
|
121
|
1049
|
|
|
|
|
1217
|
foreach my $method (keys %{$cache{$app_name}}) { |
|
1049
|
|
|
|
|
1717
|
|
122
|
1057
|
|
|
|
|
1712
|
$size += length $method; |
123
|
|
|
|
|
|
|
|
124
|
1057
|
|
|
|
|
1294
|
foreach my $path (keys %{$cache{$app_name}{$method}}) { |
|
1057
|
|
|
|
|
2130
|
|
125
|
888
|
|
|
|
|
1097
|
$size += length $path; |
126
|
888
|
|
|
|
|
2296
|
$size += length $cache{$app_name}{$method}{$path}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
2
|
|
|
2
|
|
229
|
no bytes; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
9
|
|
133
|
|
|
|
|
|
|
|
134
|
1049
|
|
|
|
|
2660
|
return $size; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub route_cache_paths { |
138
|
65
|
|
|
65
|
1
|
818
|
my $self = shift; |
139
|
65
|
50
|
|
|
|
133
|
my %cache = $self->{'cache'} ? %{$self->{'cache'}} : (); |
|
65
|
|
|
|
|
145
|
|
140
|
|
|
|
|
|
|
|
141
|
65
|
|
|
|
|
129
|
return scalar map { keys %{$_} } map { values %{$cache{$_}} } keys %cache; |
|
73
|
|
|
|
|
89
|
|
|
73
|
|
|
|
|
332
|
|
|
65
|
|
|
|
|
78
|
|
|
65
|
|
|
|
|
167
|
|
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
__END__ |