Debian Codegraph @

lib/Dancer2/Session/Simple.pm

raw
1 package Dancer2::Session::Simple;
2 # ABSTRACT: in-memory session backend for Dancer2
3 $Dancer2::Session::Simple::VERSION = '2.2.2';
4 use Moo;
5 use Dancer2::Core::Types;
6 use Carp;
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 my ($self) = @_;
15 return [ keys %{$SESSIONS} ];
16 }
17
18 sub _retrieve {
19 my ( $class, $id ) = @_;
20 my $s = $SESSIONS->{$id};
21
22 croak "Invalid session ID: $id"
23 if !defined $s;
24
25 return $s;
26 }
27
28 sub _change_id {
29 my ( $class, $old_id, $new_id ) = @_;
30
31 $SESSIONS->{$new_id} = $class->_retrieve($old_id);
32 delete $SESSIONS->{$old_id};
33 }
34
35 sub _destroy {
36 my ( $class, $id ) = @_;
37 delete $SESSIONS->{$id};
38 }
39
40 sub _flush {
41 my ( $class, $id, $data ) = @_;
42 $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 2.2.2
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) 2026 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