| 1 | package Dancer::Route::Cache; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | #ABSTRACT: route caching mechanism for L<Dancer> | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | $Dancer::Route::Cache::VERSION = '1.3522'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use base 'Dancer::Object'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Dancer::Config 'setting'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Dancer::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | Dancer::Route::Cache->attributes('size_limit', 'path_limit'); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # static | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # singleton for the current cache object | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | my $_cache; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 133 | ▶ | sub get {$_cache} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | sub reset { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | $_cache = Dancer::Route::Cache->new(); | |||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | $_cache->{size_limit} = setting('route_cache_size_limit') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | if defined setting('route_cache_size_limit'); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | $_cache->{path_limit} = setting('route_cache_path_limit') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | if defined setting('route_cache_path_limit'); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # instance | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | $self->build_size_limit($args{'size_limit'} || '10M'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'size_limit'} || '10M'
MC/DC100%1/1
$args{'size_limit'} || '10M'
$args{'size_limit'}
Truth table100%2/2
A
A= $args{'size_limit'}
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | $self->build_path_limit($args{'path_limit'} || 600); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'path_limit'} || 600
MC/DC100%1/1
$args{'path_limit'} || 600
$args{'path_limit'}
Truth table100%2/2
A
A= $args{'path_limit'}
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | sub build_path_limit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_path_limit: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | my ($self, $limit) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | if ($limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | $self->{'path_limit'} = $limit; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | return $self->{'path_limit'}; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | sub build_size_limit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_size_limit: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | my ($self, $limit) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if ($limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | $self->{'size_limit'} = $self->parse_size($limit); | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | return $self->{'size_limit'}; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 8 | ▶ | sub parse_size { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_size: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 8 | my ($self, $size) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 8 | ▶ | if ($size =~ /^(\d+)(K|M|G)?$/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 8 | my $base = $1; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 8 | ▶ | if (my $ext = $2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | ▶ | $ext eq 'K' and return $base * 1024**1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | ▶ | $ext eq 'M' and return $base * 1024**2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | $ext eq 'G' and return $base * 1024**3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | return $base; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 96 | ▶ | sub route_from_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
route_from_path: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 96 | my ($self, $method, $path, $app_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 96 | ▶ | $method && $path | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$method and $path
MC/DC0%0/2
$method and $path
$method
$path
Truth table33%1/3
A and B
A= $methodB= $path
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | or raise core_route => "Missing method or path"; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 96 | ▶ | $app_name = 'main' unless defined $app_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 96 | ▶ | return $self->{'cache'}{$app_name}{$method}{$path} || undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'cache'}{$app_name}{$method}{$path} || undef
MC/DC100%1/1
$self->{'cache'}{$app_name}{$method}{$path} || undef
$self->{'cache'}{$app_name}{$method}{$path}
Truth table100%2/2
A
A= $self->{'cache'}{$app_name}{$method}{$path}
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 548 | ▶ | sub store_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
store_path: called CC=9 SCAR=22.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 548 | my ($self, $method, $path, $route, $app_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 548 | ▶ | $method && $path && $route | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$method and $path and $route
$method and $path
MC/DC0%0/3
$method and $path and $route
$method
$path
$route
Truth table25%1/4
A and B and C
A= $methodB= $pathC= $route
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | or raise core_route => "Missing method, path or route"; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 548 | ▶ | $app_name = 'main' unless defined $app_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 548 | $self->{'cache'}{$app_name}{$method}{$path} = $route; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 548 | push @{$self->{'cache_array'}}, [$method, $path, $app_name]; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 548 | ▶ | if (my $limit = $self->size_limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 548 | ▶ | while ($self->route_cache_size() > $limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 500 | my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}}; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 500 | delete $self->{'cache'}{$app_name}{$method}{$path}; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 548 | ▶ | if (my $limit = $self->path_limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 48 | ▶ | while ($self->route_cache_paths() > $limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 16 | my ($method, $path, $app_name) = @{shift @{$self->{'cache_array'}}}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 16 | delete $self->{'cache'}{$app_name}{$method}{$path}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 1049 | ▶ | sub route_cache_size { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
route_cache_size: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1049 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 1049 | my %cache = %{$self->{'cache'}}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 1049 | my $size = 0; | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | ▶ | use bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 1049 | foreach my $app_name (keys %cache) { | |||||||||||||||||||||||||||||||||||||||||
| 119 | 1049 | $size += length $app_name; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 1049 | foreach my $method (keys %{$cache{$app_name}}) { | |||||||||||||||||||||||||||||||||||||||||
| 122 | 1057 | $size += length $method; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 1057 | foreach my $path (keys %{$cache{$app_name}{$method}}) { | |||||||||||||||||||||||||||||||||||||||||
| 125 | 888 | $size += length $path; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 888 | $size += length $cache{$app_name}{$method}{$path}; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | no bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1049 | return $size; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 65 | ▶ | sub route_cache_paths { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
route_cache_paths: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 65 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 65 | ▶ | my %cache = $self->{'cache'} ? %{$self->{'cache'}} : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 65 | return scalar map { keys %{$_} } map { values %{$cache{$_}} } keys %cache; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||||||||||||||||