File Coverage

lib/Dancer/Route/Cache.pm
Criterion Covered Total %
statement 94 94 100.0
branch 22 32 68.7
condition 9 15 60.0
subroutine 19 19 100.0
pod 6 10 60.0
total 150 170 88.2


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.3514_04'; # TRIAL
5             $Dancer::Route::Cache::VERSION = '1.351404';
6 2     2   414 use strict;
  2         5  
  2         50  
7 2     2   9 use warnings;
  2         4  
  2         38  
8 2     2   9 use Carp;
  2         2  
  2         103  
9              
10 2     2   19 use base 'Dancer::Object';
  2         5  
  2         175  
11              
12 2     2   12 use Dancer::Config 'setting';
  2         5  
  2         81  
13 2     2   10 use Dancer::Error;
  2         3  
  2         59  
14 2     2   12 use Dancer::Exception qw(:all);
  2         2  
  2         1493  
15              
16             Dancer::Route::Cache->attributes('size_limit', 'path_limit');
17              
18             # static
19              
20             # singleton for the current cache object
21             my $_cache;
22              
23 133     133 0 1207 sub get {$_cache}
24              
25             sub reset {
26 2     2 0 13 $_cache = Dancer::Route::Cache->new();
27 2 50       9 $_cache->{size_limit} = setting('route_cache_size_limit')
28             if defined setting('route_cache_size_limit');
29 2 50       5 $_cache->{path_limit} = setting('route_cache_path_limit')
30             if defined setting('route_cache_path_limit');
31             }
32              
33             # instance
34              
35             sub init {
36 3     3 1 8 my ($self, %args) = @_;
37 3   100     18 $self->build_size_limit($args{'size_limit'} || '10M');
38 3   100     18 $self->build_path_limit($args{'path_limit'} || 600);
39             }
40              
41             sub build_path_limit {
42 3     3 0 7 my ($self, $limit) = @_;
43 3 50       7 if ($limit) {
44 3         6 $self->{'path_limit'} = $limit;
45             }
46              
47 3         8 return $self->{'path_limit'};
48             }
49              
50             sub build_size_limit {
51 3     3 0 7 my ($self, $limit) = @_;
52 3 50       8 if ($limit) {
53 3         8 $self->{'size_limit'} = $self->parse_size($limit);
54             }
55              
56 3         121 return $self->{'size_limit'};
57             }
58              
59             sub parse_size {
60 8     8 1 4283 my ($self, $size) = @_;
61              
62 8 50       62 if ($size =~ /^(\d+)(K|M|G)?$/i) {
63 8         21 my $base = $1;
64 8 100       19 if (my $ext = $2) {
65 7 100       23 $ext eq 'K' and return $base * 1024**1;
66 5 100       25 $ext eq 'M' and return $base * 1024**2;
67 1 50       6 $ext eq 'G' and return $base * 1024**3;
68             }
69              
70 1         5 return $base;
71             }
72             }
73              
74             sub route_from_path {
75 96     96 1 5348 my ($self, $method, $path, $app_name) = @_;
76              
77 96 50 33     327 $method && $path
78             or raise core_route => "Missing method or path";
79              
80 96 100       198 $app_name = 'main' unless defined $app_name;
81              
82 96   100     388 return $self->{'cache'}{$app_name}{$method}{$path} || undef;
83             }
84              
85             sub store_path {
86 548     548 1 4728521 my ($self, $method, $path, $route, $app_name) = @_;
87              
88 548 50 33     4557 $method && $path && $route
      33        
89             or raise core_route => "Missing method, path or route";
90              
91 548 100       1870 $app_name = 'main' unless defined $app_name;
92              
93 548         50517 $self->{'cache'}{$app_name}{$method}{$path} = $route;
94              
95 548         1171 push @{$self->{'cache_array'}}, [$method, $path, $app_name];
  548         2518  
96              
97 548 50       2563 if (my $limit = $self->size_limit) {
98 548         1690 while ($self->route_cache_size() > $limit) {
99 500         867 my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}};
  500         747  
  500         1391  
100 500         4392 delete $self->{'cache'}{$app_name}{$method}{$path};
101             }
102             }
103              
104 548 100       1576 if (my $limit = $self->path_limit) {
105 48         122 while ($self->route_cache_paths() > $limit) {
106 16         33 my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}};
  16         20  
  16         48  
107 16         62 delete $self->{'cache'}{$app_name}{$method}{$path};
108             }
109             }
110             }
111              
112             sub route_cache_size {
113 1049     1049 1 1547 my $self = shift;
114 1049         1504 my %cache = %{$self->{'cache'}};
  1049         3074  
115 1049         1689 my $size = 0;
116              
117 2     2   15 use bytes;
  2         3  
  2         13  
118              
119 1049         2379 foreach my $app_name (keys %cache) {
120 1049         1476 $size += length $app_name;
121              
122 1049         1154 foreach my $method (keys %{$cache{$app_name}}) {
  1049         1797  
123 1057         1249 $size += length $method;
124              
125 1057         1466 foreach my $path (keys %{$cache{$app_name}{$method}}) {
  1057         2364  
126 888         1286 $size += length $path;
127 888         2437 $size += length $cache{$app_name}{$method}{$path};
128             }
129             }
130             }
131              
132              
133 2     2   194 no bytes;
  2         3  
  2         8  
134              
135 1049         3154 return $size;
136             }
137              
138             sub route_cache_paths {
139 65     65 1 923 my $self = shift;
140 65 50       139 my %cache = $self->{'cache'} ? %{$self->{'cache'}} : ();
  65         144  
141              
142 65         139 return scalar map { keys %{$_} } map { values %{$cache{$_}} } keys %cache;
  73         82  
  73         303  
  65         83  
  65         171  
143             }
144              
145             1;
146              
147             __END__