File Coverage

blib/lib/Dancer2/Session/CHI.pm
Criterion Covered Total %
statement 28 30 93.3
branch n/a
condition n/a
subroutine 10 11 90.9
pod n/a
total 38 41 92.6


line stmt bran cond sub pod time code
1             package Dancer2::Session::CHI;
2             # ABSTRACT: Dancer 2 session storage with CHI backend
3              
4 2     2   196311 use strict;
  2         7  
  2         71  
5 2     2   12 use warnings;
  2         4  
  2         64  
6              
7 2     2   18 use Moo;
  2         5  
  2         20  
8 2     2   2341 use CHI;
  2         77127  
  2         71  
9 2     2   18 use Type::Tiny;
  2         5  
  2         59  
10 2     2   11 use Types::Standard qw/ Str ArrayRef InstanceOf HashRef/;
  2         5  
  2         34  
11              
12             #
13             # Public attributes
14             #
15             has 'driver' => (
16             is => 'ro',
17             isa => Str,
18             required => 1,
19             );
20              
21             has 'driver_args' => (
22             is => 'ro',
23             isa => HashRef,
24             required => 0,
25             );
26              
27             #
28             # Private attributes
29             #
30             has _chi => (
31             is => 'lazy',
32             isa => InstanceOf ['CHI::Driver'],
33             handles => {
34             _destroy => 'remove',
35             },
36             );
37              
38             # Session methods
39             sub _retrieve {
40 7     7   87425 my ($self) = shift;
41              
42 7         124 return $self->_chi->get( @_ );
43             }
44              
45             sub _flush {
46 7     7   249152 my ($self) = shift;
47              
48 7         126 return $self->_chi->set( @_ );
49             }
50              
51             sub _build__chi {
52 1     1   19 my ($self) = @_;
53              
54             return CHI->new(
55             driver => $self->driver,
56 1         25 %{ $self->driver_args },
  1         11  
57             );
58             }
59              
60             around BUILDARGS => sub {
61             my $orig = shift;
62             my $class = shift;
63             my @args = @_;
64              
65             my %chi_args = @args;
66             delete $chi_args{ $_ } foreach qw( postponed_hooks log_cb session_dir driver );
67             push @args, 'driver_args', \%chi_args;
68             return $class->$orig( @args );
69             };
70              
71             #
72             # Role composition
73             #
74             with 'Dancer2::Core::Role::SessionFactory';
75              
76 0     0   0 sub _sessions { my $self = shift; return $self->_chi->get_keys; }
  0         0  
77              
78             sub _change_id {
79 1     1   609 my ( $self, $old_id, $new_id ) = @_;
80 1         6 $self->_flush( $new_id, $self->_retrieve( $old_id ) );
81 1         344 $self->_destroy( $old_id );
82             }
83              
84             1;
85              
86             __END__
87              
88             =pod
89              
90             =encoding UTF-8
91              
92             =head1 NAME
93              
94             Dancer2::Session::CHI - Dancer 2 session storage with CHI backend
95              
96             =head1 VERSION
97              
98             version 0.05
99              
100             =head1 SYNOPSIS
101              
102             # In Dancer 2 config.yml file
103             session: CHI
104             engines:
105             session:
106             CHI:
107             driver: FastMmap
108             root_dir: '/tmp/dancer-sessions'
109             cache_size: 1k
110              
111             =head1 DESCRIPTION
112              
113             This module implements a session factory for L<Dancer2> that stores session
114             state using L<CHI>.
115              
116             =head1 ATTRIBUTES
117              
118             =head2 driver (required)
119              
120             The backend driver CHI will use to store the session data. Any additional
121             attributes beyond the driver will be passed as additional configuration
122             parameters to CHI.
123              
124             =for Pod::Coverage method_names_here
125              
126             =head1 SEE ALSO
127              
128             =over 4
129              
130             =item * L<CHI>
131              
132             =item * L<Dancer2>
133              
134             =back
135              
136             =head1 CREDITS
137              
138             This is heavily based on L<Dancer2::Session::Memcached> by David Golden and
139             Yanick Champoux.
140              
141             =head2 Contributors
142              
143             The following people have contributed to C<Dancer2::Session::CHI> in some way,
144             either through bug reports, code, suggestions, or moral support:
145              
146             =over
147              
148             =item andk
149              
150             =item Mohammad S Anwar
151              
152             =back
153              
154             =head1 AUTHOR
155              
156             Jason A. Crome <cromedome@cpan.org>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2023 by Jason A. Crome.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =cut