1 package Plack::Session;
6 our $AUTHORITY = 'cpan:STEVAN';
8 use Plack::Util::Accessor qw( id expired _manager );
11 my($class, $request, $manager) = @_;
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 );
18 $id = $manager->state->generate($request);
19 return $class->new( id => $id, _stash => {}, _manager => $manager, _changed=> 1 );
24 my ($class, %params) = @_;
25 bless { %params } => $class;
36 my ($self, $key) = @_;
37 $self->{_stash}{$key};
41 my ($self, $key, $value) = @_;
43 $self->{_stash}{$key} = $value;
47 my ($self, $key) = @_;
49 delete $self->{_stash}{$key};
54 keys %{$self->{_stash}};
57 ## Lifecycle Management
69 $self->_manager->store->cleanup($self->id);
71 $self->_manager->store->store($self->id, $self);
74 $self->{_changed} = 0;
79 $self->{_changed} > 0;
83 my ($self, $response) = @_;
85 $self->commit if $self->is_changed || $self->expired;
87 $self->_manager->state->expire_session_id($self->id, $response);
89 $self->_manager->state->finalize($self->id, $response, $self);
101 Plack::Session - Middleware for session management
107 my $store = Plack::Session::Store->new;
108 my $state = Plack::Session::State->new;
110 my $s = Plack::Session->new(
113 request => Plack::Request->new( $env )
120 This is the core session object, you probably want to look
121 at L<Plack::Middleware::Session>, unless you are writing your
122 own session middleware component.
128 =item B<new ( %params )>
130 The constructor expects keys in C<%params> for I<state>,
131 I<store> and I<request>. The I<request> param is expected to be
132 a L<Plack::Request> instance or an object with an equivalent
137 This is the accessor for the session id.
141 This is expected to be a L<Plack::Session::State> instance or
142 an object with an equivalent interface.
146 This is expected to be a L<Plack::Session::Store> instance or
147 an object with an equivalent interface.
151 =head2 Session Data Management
153 These methods allows you to read and write the session data like
154 Perl's normal hash. The operation is not synced to the storage until
155 you call C<finalize> on it.
159 =item B<get ( $key )>
161 =item B<set ( $key, $value )>
163 =item B<remove ( $key )>
169 =head2 Session Lifecycle Management
175 This method synchronizes the session data to the data store, without
176 waiting for the response final phase.
180 This method can be called to expire the current session id. It marks
181 the session as expire and call the C<cleanup> method on the C<store>
182 and the C<expire_session_id> method on the C<state>.
184 =item B<finalize ( $manager, $response )>
186 This method should be called at the end of the response cycle. It will
187 call the C<store> method on the C<store> and the C<expire_session_id>
188 method on the C<state>. The C<$response> is expected to be a
189 L<Plack::Response> instance or an object with an equivalent interface.
195 All complex software has bugs lurking in it, and this module is no
196 exception. If you find a bug please either email me, or add the bug
201 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
203 =head1 COPYRIGHT AND LICENSE
205 Copyright 2009, 2010 Infinity Interactive, Inc.
207 L<http://www.iinteractive.com>
209 This library is free software; you can redistribute it and/or modify
210 it under the same terms as Perl itself.