Reworked Session to make the expiration a store's responsibility and
[catagits/Web-Session.git] / lib / Plack / Session.pm
CommitLineData
06190e8b 1package Plack::Session;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
caf3bd90 8use Plack::Util::Accessor qw( id expired _manager );
4a0cb5a0 9
10sub fetch_or_create {
11 my($class, $request, $manager) = @_;
12
caf3bd90 13 my($id, $session);
14 if ($id = $manager->state->extract($request) and
15 $session = $manager->store->fetch($id)) {
16 return $class->new( id => $id, _stash => $session, _manager => $manager, _changed => 0 );
4a0cb5a0 17 } else {
18 $id = $manager->state->generate($request);
caf3bd90 19 return $class->new( id => $id, _stash => {}, _manager => $manager, _changed=> 1 );
4a0cb5a0 20 }
21}
06190e8b 22
23sub new {
24 my ($class, %params) = @_;
ac4892f4 25 bless { %params } => $class;
06190e8b 26}
27
28## Data Managment
29
4a0cb5a0 30sub dump {
31 my $self = shift;
32 $self->{_stash};
33}
34
06190e8b 35sub get {
36 my ($self, $key) = @_;
4a0cb5a0 37 $self->{_stash}{$key};
06190e8b 38}
39
40sub set {
41 my ($self, $key, $value) = @_;
caf3bd90 42 $self->{_changed}++;
4a0cb5a0 43 $self->{_stash}{$key} = $value;
06190e8b 44}
45
46sub remove {
47 my ($self, $key) = @_;
caf3bd90 48 $self->{_changed}++;
4a0cb5a0 49 delete $self->{_stash}{$key};
50}
51
52sub keys {
53 my $self = shift;
54 keys %{$self->{_stash}};
06190e8b 55}
56
57## Lifecycle Management
58
59sub expire {
60 my $self = shift;
4a0cb5a0 61 $self->{_stash} = {};
caf3bd90 62 $self->expired(1);
63}
64
65sub commit {
66 my $self = shift;
67
68 if ($self->expired) {
69 $self->_manager->store->cleanup($self->id);
70 } else {
71 $self->_manager->store->store($self->id, $self);
72 }
73
74 $self->{_changed} = 0;
75}
76
77sub is_changed {
78 my $self = shift;
79 $self->{_changed} > 0;
06190e8b 80}
81
82sub finalize {
bd992981 83 my ($self, $response) = @_;
caf3bd90 84
85 $self->commit if $self->is_changed || $self->expired;
86 if ($self->expired) {
87 $self->_manager->state->expire_session_id($self->id, $response);
88 } else {
89 $self->_manager->state->finalize($self->id, $response, $self);
90 }
06190e8b 91}
92
ac4892f4 931;
94
95__END__
96
97=pod
98
99=head1 NAME
100
101Plack::Session - Middleware for session management
102
103=head1 SYNOPSIS
104
105 use Plack::Session;
106
3d92cf47 107 my $store = Plack::Session::Store->new;
108 my $state = Plack::Session::State->new;
109
110 my $s = Plack::Session->new(
111 store => $store,
112 state => $state,
113 request => Plack::Request->new( $env )
114 );
115
116 # ...
117
ac4892f4 118=head1 DESCRIPTION
119
3d92cf47 120This is the core session object, you probably want to look
d5abacda 121at L<Plack::Middleware::Session>, unless you are writing your
3d92cf47 122own session middleware component.
123
ac4892f4 124=head1 METHODS
125
126=over 4
127
128=item B<new ( %params )>
129
43f34c01 130The constructor expects keys in C<%params> for I<state>,
131I<store> and I<request>. The I<request> param is expected to be
132a L<Plack::Request> instance or an object with an equivalent
133interface.
134
ac4892f4 135=item B<id>
136
43f34c01 137This is the accessor for the session id.
138
ac4892f4 139=item B<state>
140
43f34c01 141This is expected to be a L<Plack::Session::State> instance or
142an object with an equivalent interface.
143
ac4892f4 144=item B<store>
145
43f34c01 146This is expected to be a L<Plack::Session::Store> instance or
147an object with an equivalent interface.
148
ac4892f4 149=back
150
4a0cb5a0 151=head2 Session Data Management
43f34c01 152
4a0cb5a0 153These methods allows you to read and write the session data like
154Perl's normal hash. The operation is not synced to the storage until
155you call C<finalize> on it.
43f34c01 156
ac4892f4 157=over 4
158
159=item B<get ( $key )>
160
161=item B<set ( $key, $value )>
162
163=item B<remove ( $key )>
164
4a0cb5a0 165=item B<keys>
166
ac4892f4 167=back
168
43f34c01 169=head2 Session Lifecycle Management
170
ac4892f4 171=over 4
172
caf3bd90 173=item B<commit>
174
175This method synchronizes the session data to the data store, without
176waiting for the response final phase.
177
ac4892f4 178=item B<expire>
179
caf3bd90 180This method can be called to expire the current session id. It marks
181the session as expire and call the C<cleanup> method on the C<store>
182and the C<expire_session_id> method on the C<state>.
43f34c01 183
caf3bd90 184=item B<finalize ( $manager, $response )>
ac4892f4 185
caf3bd90 186This method should be called at the end of the response cycle. It will
187call the C<store> method on the C<store> and the C<expire_session_id>
188method on the C<state>. The C<$response> is expected to be a
189L<Plack::Response> instance or an object with an equivalent interface.
43f34c01 190
ac4892f4 191=back
192
193=head1 BUGS
194
195All complex software has bugs lurking in it, and this module is no
196exception. If you find a bug please either email me, or add the bug
197to cpan-RT.
198
199=head1 AUTHOR
200
201Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
202
203=head1 COPYRIGHT AND LICENSE
204
000c696e 205Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 206
207L<http://www.iinteractive.com>
208
209This library is free software; you can redistribute it and/or modify
210it under the same terms as Perl itself.
211
212=cut
213