File Coverage

blib/lib/Mojolicious/Sessions/ThreeS/Storage/CHI.pm
Criterion Covered Total %
statement 14 17 82.3
branch 2 4 50.0
condition n/a
subroutine 4 5 80.0
pod 3 3 100.0
total 23 29 79.3


line stmt bran cond sub pod time code
1             package Mojolicious::Sessions::ThreeS::Storage::CHI;
2             $Mojolicious::Sessions::ThreeS::Storage::CHI::VERSION = '0.002';
3 1     1   365 use Mojo::Base qw/Mojolicious::Sessions::ThreeS::Storage/;
  1         2  
  1         4  
4              
5 1     1   700 use JSON;
  1         7549  
  1         3  
6              
7             =head1 NAME
8              
9             Mojolicious::Sessions::ThreeS::Storage::CHI - An adapter to store sessions in a CHI instance.
10              
11             =head1 SYNOPSIS
12              
13             my $storage = Mojolicious::Sessions::ThreeS::Storage::CHI->new({ chi => .. a CHI instance .. });
14              
15             # Use $storage in the Mojolicious::Sessions::ThreeS instance (or through the plugin):
16              
17             $app->sessions( Mojolicious::Sessions::ThreeS->new({ storage => $storage , state => ... } );
18              
19             Note that you WILL have to depend on CHI and on JSON in your application to use this storage.
20              
21             This distribution does not add these to the runtime dependencies to avoid clutter.
22              
23             =cut
24              
25             has 'chi';
26             has 'json' => sub{
27             my $json = JSON->new();
28             $json->ascii( 1 );
29             # Encode stuff in ascii so there is no risk
30             # of bad decoding.
31             return $json;
32             };
33              
34             =head2 get_session
35              
36             See L
37              
38             =cut
39              
40             sub get_session{
41 5     5 1 21 my ($self, $session_id) = @_;
42 5         10 my $value = $self->chi->get( $session_id );
43 5 50       353 unless( $value ){ return; }
  0         0  
44 5         16 return $self->json()->decode( $value );
45             }
46              
47             =head2 store_session
48              
49             See L
50              
51             =cut
52              
53             sub store_session {
54 7     7 1 26 my ( $self, $session_id, $session ) = @_;
55 7         8 my $expires = $session->{expires};
56 7         13 my $value = $self->json()->encode($session);
57 7 50       58 $self->chi->set(
58             $session_id,
59             $value,
60             (
61             $expires ? { expires_at => $expires, expires_variance => 0.15 } : ()
62             )
63             );
64             }
65              
66             =head2 remove_session_id
67              
68             See L
69              
70             =cut
71              
72             sub remove_session_id{
73 0     0 1   my ($self, $session_id) = @_;
74 0           $self->chi->remove( $session_id );
75             }
76              
77             1;