File Coverage

blib/lib/PAGI/Middleware/Session/Store/Memory.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 33 33 100.0


line stmt bran cond sub pod time code
1             package PAGI::Middleware::Session::Store::Memory;
2              
3 3     3   786 use strict;
  3         5  
  3         98  
4 3     3   11 use warnings;
  3         4  
  3         131  
5 3     3   350 use parent 'PAGI::Middleware::Session::Store';
  3         324  
  3         16  
6 3     3   150 use Future;
  3         4  
  3         588  
7              
8             =head1 NAME
9              
10             PAGI::Middleware::Session::Store::Memory - In-memory session store
11              
12             =head1 SYNOPSIS
13              
14             use PAGI::Middleware::Session::Store::Memory;
15              
16             my $store = PAGI::Middleware::Session::Store::Memory->new();
17              
18             # All methods return Futures
19             await $store->set('session_id', { user_id => 123 });
20             my $data = await $store->get('session_id');
21             await $store->delete('session_id');
22              
23             # For testing
24             PAGI::Middleware::Session::Store::Memory->clear_all();
25              
26             =head1 DESCRIPTION
27              
28             Implements the L interface using a
29             package-level hash. Sessions are shared across all instances within the
30             same process but are not shared between workers and are lost on restart.
31              
32             B This store is suitable for development and single-process
33             deployments only. For production multi-worker deployments, use a store
34             backed by Redis, a database, or another shared storage.
35              
36             =cut
37              
38             my %sessions;
39              
40             =head1 METHODS
41              
42             =head2 get
43              
44             my $future = $store->get($id);
45              
46             Returns a Future resolving to the session hashref, or undef if no
47             session exists for the given ID.
48              
49             =cut
50              
51             sub get {
52 14     14 1 1228 my ($self, $id) = @_;
53              
54 14         72 return Future->done($sessions{$id});
55             }
56              
57             =head2 set
58              
59             my $future = $store->set($id, $data);
60              
61             Stores the session data hashref under the given ID. Returns a Future
62             resolving to the transport value (the session ID for server-side stores,
63             or encoded data for cookie stores).
64              
65             =cut
66              
67             sub set {
68 22     22 1 768 my ($self, $id, $data) = @_;
69              
70 22         46 $sessions{$id} = $data;
71 22         56 return Future->done($id);
72             }
73              
74             =head2 delete
75              
76             my $future = $store->delete($id);
77              
78             Removes the session for the given ID. Returns a Future resolving to 1.
79              
80             =cut
81              
82             sub delete {
83 4     4 1 405 my ($self, $id) = @_;
84              
85 4         45 delete $sessions{$id};
86 4         17 return Future->done(1);
87             }
88              
89             =head2 clear_all
90              
91             PAGI::Middleware::Session::Store::Memory->clear_all();
92              
93             Class method that removes all sessions from the in-memory store.
94             Useful for test cleanup.
95              
96             =cut
97              
98             sub clear_all {
99 17     17 1 215541 %sessions = ();
100             }
101              
102             1;
103              
104             __END__