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.3520'; |
5
|
2
|
|
|
2
|
|
460
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
63
|
|
6
|
2
|
|
|
2
|
|
14
|
use warnings; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
52
|
|
7
|
2
|
|
|
2
|
|
9
|
use Carp; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
115
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
11
|
use base 'Dancer::Object'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
185
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
25
|
use Dancer::Config 'setting'; |
|
2
|
|
|
|
|
18
|
|
|
2
|
|
|
|
|
122
|
|
12
|
2
|
|
|
2
|
|
22
|
use Dancer::Error; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
51
|
|
13
|
2
|
|
|
2
|
|
21
|
use Dancer::Exception qw(:all); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1855
|
|
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
|
986
|
sub get {$_cache} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub reset { |
25
|
2
|
|
|
2
|
0
|
16
|
$_cache = Dancer::Route::Cache->new(); |
26
|
2
|
50
|
|
|
|
10
|
$_cache->{size_limit} = setting('route_cache_size_limit') |
27
|
|
|
|
|
|
|
if defined setting('route_cache_size_limit'); |
28
|
2
|
50
|
|
|
|
7
|
$_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
|
7
|
my ($self, %args) = @_; |
36
|
3
|
|
100
|
|
|
22
|
$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
|
|
|
|
|
7
|
$self->{'path_limit'} = $limit; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
9
|
return $self->{'path_limit'}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub build_size_limit { |
50
|
3
|
|
|
3
|
0
|
11
|
my ($self, $limit) = @_; |
51
|
3
|
50
|
|
|
|
8
|
if ($limit) { |
52
|
3
|
|
|
|
|
9
|
$self->{'size_limit'} = $self->parse_size($limit); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
8
|
return $self->{'size_limit'}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub parse_size { |
59
|
8
|
|
|
8
|
1
|
4994
|
my ($self, $size) = @_; |
60
|
|
|
|
|
|
|
|
61
|
8
|
50
|
|
|
|
78
|
if ($size =~ /^(\d+)(K|M|G)?$/i) { |
62
|
8
|
|
|
|
|
21
|
my $base = $1; |
63
|
8
|
100
|
|
|
|
28
|
if (my $ext = $2) { |
64
|
7
|
100
|
|
|
|
28
|
$ext eq 'K' and return $base * 1024**1; |
65
|
5
|
100
|
|
|
|
29
|
$ext eq 'M' and return $base * 1024**2; |
66
|
1
|
50
|
|
|
|
6
|
$ext eq 'G' and return $base * 1024**3; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
5
|
return $base; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub route_from_path { |
74
|
96
|
|
|
96
|
1
|
5621
|
my ($self, $method, $path, $app_name) = @_; |
75
|
|
|
|
|
|
|
|
76
|
96
|
50
|
33
|
|
|
705
|
$method && $path |
77
|
|
|
|
|
|
|
or raise core_route => "Missing method or path"; |
78
|
|
|
|
|
|
|
|
79
|
96
|
100
|
|
|
|
206
|
$app_name = 'main' unless defined $app_name; |
80
|
|
|
|
|
|
|
|
81
|
96
|
|
100
|
|
|
400
|
return $self->{'cache'}{$app_name}{$method}{$path} || undef; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub store_path { |
85
|
548
|
|
|
548
|
1
|
5554755
|
my ($self, $method, $path, $route, $app_name) = @_; |
86
|
|
|
|
|
|
|
|
87
|
548
|
50
|
33
|
|
|
2824
|
$method && $path && $route |
|
|
|
33
|
|
|
|
|
88
|
|
|
|
|
|
|
or raise core_route => "Missing method, path or route"; |
89
|
|
|
|
|
|
|
|
90
|
548
|
100
|
|
|
|
1215
|
$app_name = 'main' unless defined $app_name; |
91
|
|
|
|
|
|
|
|
92
|
548
|
|
|
|
|
60343
|
$self->{'cache'}{$app_name}{$method}{$path} = $route; |
93
|
|
|
|
|
|
|
|
94
|
548
|
|
|
|
|
1038
|
push @{$self->{'cache_array'}}, [$method, $path, $app_name]; |
|
548
|
|
|
|
|
2073
|
|
95
|
|
|
|
|
|
|
|
96
|
548
|
50
|
|
|
|
1841
|
if (my $limit = $self->size_limit) { |
97
|
548
|
|
|
|
|
1201
|
while ($self->route_cache_size() > $limit) { |
98
|
500
|
|
|
|
|
689
|
my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}}; |
|
500
|
|
|
|
|
641
|
|
|
500
|
|
|
|
|
1234
|
|
99
|
500
|
|
|
|
|
4684
|
delete $self->{'cache'}{$app_name}{$method}{$path}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
548
|
100
|
|
|
|
1212
|
if (my $limit = $self->path_limit) { |
104
|
48
|
|
|
|
|
99
|
while ($self->route_cache_paths() > $limit) { |
105
|
16
|
|
|
|
|
30
|
my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}}; |
|
16
|
|
|
|
|
26
|
|
|
16
|
|
|
|
|
54
|
|
106
|
16
|
|
|
|
|
59
|
delete $self->{'cache'}{$app_name}{$method}{$path}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub route_cache_size { |
112
|
1049
|
|
|
1049
|
1
|
1609
|
my $self = shift; |
113
|
1049
|
|
|
|
|
1662
|
my %cache = %{$self->{'cache'}}; |
|
1049
|
|
|
|
|
2524
|
|
114
|
1049
|
|
|
|
|
1619
|
my $size = 0; |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
|
2
|
|
26
|
use bytes; |
|
2
|
|
|
|
|
15
|
|
|
2
|
|
|
|
|
28
|
|
117
|
|
|
|
|
|
|
|
118
|
1049
|
|
|
|
|
1870
|
foreach my $app_name (keys %cache) { |
119
|
1049
|
|
|
|
|
1440
|
$size += length $app_name; |
120
|
|
|
|
|
|
|
|
121
|
1049
|
|
|
|
|
1233
|
foreach my $method (keys %{$cache{$app_name}}) { |
|
1049
|
|
|
|
|
1740
|
|
122
|
1057
|
|
|
|
|
1268
|
$size += length $method; |
123
|
|
|
|
|
|
|
|
124
|
1057
|
|
|
|
|
1249
|
foreach my $path (keys %{$cache{$app_name}{$method}}) { |
|
1057
|
|
|
|
|
2112
|
|
125
|
888
|
|
|
|
|
1233
|
$size += length $path; |
126
|
888
|
|
|
|
|
2397
|
$size += length $cache{$app_name}{$method}{$path}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
2
|
|
|
2
|
|
266
|
no bytes; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
133
|
|
|
|
|
|
|
|
134
|
1049
|
|
|
|
|
2754
|
return $size; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub route_cache_paths { |
138
|
65
|
|
|
65
|
1
|
797
|
my $self = shift; |
139
|
65
|
50
|
|
|
|
139
|
my %cache = $self->{'cache'} ? %{$self->{'cache'}} : (); |
|
65
|
|
|
|
|
137
|
|
140
|
|
|
|
|
|
|
|
141
|
65
|
|
|
|
|
141
|
return scalar map { keys %{$_} } map { values %{$cache{$_}} } keys %cache; |
|
73
|
|
|
|
|
142
|
|
|
73
|
|
|
|
|
365
|
|
|
65
|
|
|
|
|
94
|
|
|
65
|
|
|
|
|
160
|
|
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
__END__ |