File Coverage

blib/lib/Dancer2/Session/Simple.pm
Criterion Covered Total %
statement 20 23 86.9
branch 2 2 100.0
condition n/a
subroutine 7 8 87.5
pod n/a
total 29 33 87.8


line stmt bran cond sub pod time code
1             package Dancer2::Session::Simple;
2             # ABSTRACT: in-memory session backend for Dancer2
3             $Dancer2::Session::Simple::VERSION = '0.400001';
4 113     113   66655 use Moo;
  113         309  
  113         1107  
5 113     113   45262 use Dancer2::Core::Types;
  113         307  
  113         1012  
6 113     113   1454782 use Carp;
  113         355  
  113         39389  
7              
8             with 'Dancer2::Core::Role::SessionFactory';
9              
10             # The singleton that contains all the session objects created
11             my $SESSIONS = {};
12              
13             sub _sessions {
14 0     0   0 my ($self) = @_;
15 0         0 return [ keys %{$SESSIONS} ];
  0         0  
16             }
17              
18             sub _retrieve {
19 29     29   88 my ( $class, $id ) = @_;
20 29         89 my $s = $SESSIONS->{$id};
21              
22 29 100       489 croak "Invalid session ID: $id"
23             if !defined $s;
24              
25 28         146 return $s;
26             }
27              
28             sub _change_id {
29 2     2   10 my ( $class, $old_id, $new_id ) = @_;
30              
31 2         9 $SESSIONS->{$new_id} = $class->_retrieve($old_id);
32 2         9 delete $SESSIONS->{$old_id};
33             }
34              
35             sub _destroy {
36 7     7   28 my ( $class, $id ) = @_;
37 7         31 delete $SESSIONS->{$id};
38             }
39              
40             sub _flush {
41 10062     10062   18937 my ( $class, $id, $data ) = @_;
42 10062         30758 $SESSIONS->{$id} = $data;
43             }
44              
45             1;
46              
47             __END__
48              
49             =pod
50              
51             =encoding UTF-8
52              
53             =head1 NAME
54              
55             Dancer2::Session::Simple - in-memory session backend for Dancer2
56              
57             =head1 VERSION
58              
59             version 0.400001
60              
61             =head1 DESCRIPTION
62              
63             This module implements a very simple session backend, holding all session data
64             in memory. This means that sessions are volatile, and no longer exist when the
65             process exits. This module is likely to be most useful for testing purposes.
66              
67             =head1 DISCLAIMER
68              
69             This session factory should not be used in production and is only for
70             single-process application workers. As the sessions objects are stored
71             in-memory, they cannot be shared among multiple workers.
72              
73             =head1 CONFIGURATION
74              
75             The setting B<session> should be set to C<Simple> in order to use this session
76             engine in a Dancer2 application.
77              
78             =head1 SEE ALSO
79              
80             See L<Dancer2::Core::Session> for details about session usage in route handlers.
81              
82             =head1 AUTHOR
83              
84             Dancer Core Developers
85              
86             =head1 COPYRIGHT AND LICENSE
87              
88             This software is copyright (c) 2023 by Alexis Sukrieh.
89              
90             This is free software; you can redistribute it and/or modify it under
91             the same terms as the Perl 5 programming language system itself.
92              
93             =cut