File Coverage

blib/lib/Mojolicious/Sessions/ThreeS/Storage/CHI.pm
Criterion Covered Total %
statement 17 19 89.4
branch 4 4 100.0
condition n/a
subroutine 4 5 80.0
pod 3 3 100.0
total 28 31 90.3


line stmt bran cond sub pod time code
1             package Mojolicious::Sessions::ThreeS::Storage::CHI;
2             $Mojolicious::Sessions::ThreeS::Storage::CHI::VERSION = '0.004';
3 2     2   187280 use Mojo::Base qw/Mojolicious::Sessions::ThreeS::Storage/;
  2         3  
  2         11  
4              
5 2     2   1432 use JSON;
  2         16137  
  2         8  
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             =head1 ATTRIBUTES
24              
25             =head2 default_expiration
26              
27             Default expiration length (in seconds) in case you dont define a default session expiration
28             at higher level. This is required to avoid CHI cache overflowing.
29              
30             Defaults to 43200 (12 hours).
31              
32             =cut
33              
34             has 'default_expiration' => 43200;
35             has 'chi';
36             has 'json' => sub{
37             my $json = JSON->new();
38             $json->ascii( 1 );
39             # Encode stuff in ascii so there is no risk
40             # of bad decoding.
41             return $json;
42             };
43              
44             =head1 METHODS
45              
46             =head2 get_session
47              
48             See L
49              
50             =cut
51              
52             sub get_session{
53 9     9 1 750 my ($self, $session_id) = @_;
54 9         23 my $value = $self->chi->get( $session_id );
55 9 100       700 unless( $value ){ return; }
  2         6  
56 7         17 return $self->json()->decode( $value );
57             }
58              
59             =head2 store_session
60              
61             See L
62              
63             =cut
64              
65             sub store_session {
66 9     9 1 58853 my ( $self, $session_id, $session ) = @_;
67 9         13 my $expires = $session->{expires};
68 9         23 my $value = $self->json()->encode($session);
69 9 100       87 unless( defined $expires ){
70 1         4 $expires = time() + $self->default_expiration();
71             }
72             $self->chi->set(
73 9         35 $session_id,
74             $value,
75             { expires_at => $expires, expires_variance => 0.15 }
76             );
77             }
78              
79             =head2 remove_session_id
80              
81             See L
82              
83             =cut
84              
85             sub remove_session_id{
86 0     0 1   my ($self, $session_id) = @_;
87 0           $self->chi->remove( $session_id );
88             }
89              
90             1;