blib/lib/Dancer/Route/Cache.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 181 SCAR 50.5
CC
12
Cov
0%
store_path
20.0
reset
12.0
parse_size
12.0
CRAP 156.0
1 package Dancer::Route::Cache;
2 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: route caching mechanism for L<Dancer>
4 $Dancer::Route::Cache::VERSION = '1.3520';
5 use strict;
6 use warnings;
7 use Carp;
8  
9 use base 'Dancer::Object';
10  
11 use Dancer::Config 'setting';
12 use Dancer::Error;
13 use Dancer::Exception qw(:all);
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 sub get {$_cache}
23  
24 sub reset {
25 $_cache = Dancer::Route::Cache->new();
26 $_cache->{size_limit} = setting('route_cache_size_limit')
27 if defined setting('route_cache_size_limit');
28 $_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 my ($self, %args) = @_;
36 $self->build_size_limit($args{'size_limit'} || '10M');
37 $self->build_path_limit($args{'path_limit'} || 600);
38 }
39  
40 sub build_path_limit {
41 my ($self, $limit) = @_;
42 if ($limit) {
43 $self->{'path_limit'} = $limit;
44 }
45  
46 return $self->{'path_limit'};
47 }
48  
49 sub build_size_limit {
50 my ($self, $limit) = @_;
51 if ($limit) {
52 $self->{'size_limit'} = $self->parse_size($limit);
53 }
54  
55 return $self->{'size_limit'};
56 }
57  
58 sub parse_size {
59 my ($self, $size) = @_;
60  
61 if ($size =~ /^(\d+)(K|M|G)?$/i) {
62 my $base = $1;
63 if (my $ext = $2) {
64 $ext eq 'K' and return $base * 1024**1;
65 $ext eq 'M' and return $base * 1024**2;
66 $ext eq 'G' and return $base * 1024**3;
67 }
68  
69 return $base;
70 }
71 }
72  
73 sub route_from_path {
74 my ($self, $method, $path, $app_name) = @_;
75  
76 $method && $path
77 or raise core_route => "Missing method or path";
78  
79 $app_name = 'main' unless defined $app_name;
80  
81 return $self->{'cache'}{$app_name}{$method}{$path} || undef;
82 }
83  
84 sub store_path {
85 my ($self, $method, $path, $route, $app_name) = @_;
86  
87 $method && $path && $route
88 or raise core_route => "Missing method, path or route";
89  
90 $app_name = 'main' unless defined $app_name;
91  
92 $self->{'cache'}{$app_name}{$method}{$path} = $route;
93  
94 push @{$self->{'cache_array'}}, [$method, $path, $app_name];
95  
96 if (my $limit = $self->size_limit) {
97 while ($self->route_cache_size() > $limit) {
98 my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}};
99 delete $self->{'cache'}{$app_name}{$method}{$path};
100 }
101 }
102  
103 if (my $limit = $self->path_limit) {
104 while ($self->route_cache_paths() > $limit) {
105 my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}};
106 delete $self->{'cache'}{$app_name}{$method}{$path};
107 }
108 }
109 }
110  
111 sub route_cache_size {
112 my $self = shift;
113 my %cache = %{$self->{'cache'}};
114 my $size = 0;
115  
116 use bytes;
117  
118 foreach my $app_name (keys %cache) {
119 $size += length $app_name;
120  
121 foreach my $method (keys %{$cache{$app_name}}) {
122 $size += length $method;
123  
124 foreach my $path (keys %{$cache{$app_name}{$method}}) {
125 $size += length $path;
126 $size += length $cache{$app_name}{$method}{$path};
127 }
128 }
129 }
130  
131  
132 no bytes;
133  
134 return $size;
135 }
136  
137 sub route_cache_paths {
138 my $self = shift;
139 my %cache = $self->{'cache'} ? %{$self->{'cache'}} : ();
140  
141 return scalar map { keys %{$_} } map { values %{$cache{$_}} } keys %cache;
142 }
143  
144 1;
145  
146 __END__