File Coverage

blib/lib/Mojo/DB/Connector/Role/Cache.pm
Criterion Covered Total %
statement 12 24 50.0
branch 0 4 0.0
condition 0 2 0.0
subroutine 4 5 80.0
pod 1 1 100.0
total 17 36 47.2


line stmt bran cond sub pod time code
1             package Mojo::DB::Connector::Role::Cache;
2 1     1   1164 use Mojo::Base -role;
  1         2  
  1         7  
3 1     1   400 use List::Util qw(pairs unpairs);
  1         2  
  1         121  
4 1     1   473 use Mojo::Cache;
  1         409  
  1         8  
5 1     1   32 use Mojo::Util ();
  1         2  
  1         278  
6              
7             requires qw(_config _to_url new_connection);
8              
9             has cache => sub { Mojo::Cache->new };
10              
11             sub cached_connection {
12 0     0 1   my $self = shift;
13 0           my %config = $self->_config(@_);
14 0           my $mojo_url = $self->_to_url(%config);
15              
16             # sort parameters so cached urls are the same
17             $mojo_url->query(
18             unpairs
19 0 0         sort { $a->[0] cmp $b->[0] or $a->[1] cmp $b->[1] }
20 0           pairs @{ $mojo_url->query->pairs }
  0            
21             );
22 0   0       my $cache_url = $mojo_url->userinfo(Mojo::Util::sha1_sum($mojo_url->userinfo // ''))->to_unsafe_string;
23              
24 0           my $connection;
25 0 0         unless ($connection = $self->cache->get($cache_url)) {
26 0           $connection = $self->new_connection(@_);
27 0           $self->cache->set($cache_url => $connection);
28             }
29              
30 0           return $connection;
31             }
32              
33             1;
34             __END__