Slick/Annotation.pm

filter: Statementstmt 48.4% 16 / 33 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 1 totaltotal 42.0% 21 / 50 CC 5 SCAR 21.8
CC
5
Cov
39%
__ANON__
20.0
cacheable
6.0
BEGIN
1.0
CRAP 8.8
« Slick.pm ↑ Summary Slick/Context.pm »
1 package Slick::Annotation;
2  
3 1 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::Tiny qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @EXPORT_OK = qw(cacheable);
11  
12 0 ▶sub cacheable {
Subroutine cacheable: not called CC=2 SCAR=17.9
Pod undocumented
13 0 my $cache = shift;
14 0 my $code = shift;
15 0 ▶ my $timeout = shift // 300; # Default cache is 5 minutes
Condition0%0/2
shift() // 300
l!l
00
MC/DC0%0/1
shift() // 300
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
16  
17 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
18 0 my ( $app, $context ) = @_;
19  
20 0 state $cache_obj = $app->cache($cache);
21  
22 0 ▶ if ($cache_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_obj) { } 0 0 0
23 0 ▶ if ( $cache_obj->get( $context->request->uri ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_obj->get($context->request->uri)) { } 0 0 0
24 0 my $response =
25 decode_json( $cache_obj->get( $context->request->uri ) );
26  
27 0 $context->from_psgi($response);
28 }
29 else {
30 0 $code->( $app, $context );
31  
32 0 my $json = encode_json $context->to_psgi;
33  
34 0 ▶ if ( blessed( $cache_obj->{_executor} ) =~ /Memcached/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($cache_obj->{'_executor'}) =~ /Memcached/ux) { } 0 0 0
35 0 $cache_obj->set(
36 $context->request->uri => $json => $timeout );
37 }
38 else {
39 0 $cache_obj->set(
40 $context->request->uri => $json => EX => $timeout );
41 }
42 }
43 }
44 else {
45 0 carp
46 qq{Attempted to use cache $cache to cache route but cache does not exist.};
47 0 $code->( $app, $context );
48 }
49 }
50 0 }
51  
52 1 1;
53  
54 =encoding utf8
55  
56 =head1 NAME
57  
58 Slick::Annotation
59  
60 =head1 SYNOPSIS
61  
62 A functional module for "annotations", simply functions that can compose
63 nicely to add functionality to routes.
64  
65 =head1 cacheable
66  
67 use 5.036;
68  
69 use Slick;
70 use Slick::Annotation qw(cacheable);
71  
72 my $s = Slick->new;
73  
74 # See Redis and Cache::Memcached on CPAN for arguments
75  
76 # Create a Redis instance
77 $s->cache(
78 my_redis => type => 'redis', # Slick Arguments
79 server => '127.0.0.1:6379' # Cache::Memcached arguments
80 );
81  
82 # Use your cache to cache your route
83 $s->get(
84 '/foobar' => cacheable(
85 'my_redis', # cache name
86 sub {
87 my ( $app, $context ) = @_;
88 return $context->json( { foo => 'bar' } );
89 }
90 )
91 );
92  
93 $s->run;
94  
95 Declares a route sub-routine as "cacheable" meaning it will
96 always return the same response, and retrieve that response from a
97 specified cache. See L<Slick::Cache> for more information about
98 caching.
99  
100 =head1 See also
101  
102 =over2
103  
104 =item * L<Slick::CacheExecutor>
105  
106 =item * L<Slick::CacheExecutor::Redis>
107  
108 =item * L<Slick::CacheExecutor::Memcached>
109  
110 =item * L<Slick::Context>
111  
112 =item * L<Slick::Database>
113  
114 =item * L<Slick::DatabaseExecutor>
115  
116 =item * L<Slick::DatabaseExecutor::MySQL>
117  
118 =item * L<Slick::DatabaseExecutor::Pg>
119  
120 =item * L<Slick::EventHandler>
121  
122 =item * L<Slick::Events>
123  
124 =item * L<Slick::Methods>
125  
126 =item * L<Slick::RouteMap>
127  
128 =item * L<Slick::Util>
129  
130 =back
131  
132 =cut
« Slick.pm ↑ Summary Slick/Context.pm »