1 package Plack::Session;
6 our $AUTHORITY = 'cpan:STEVAN';
8 use Plack::Util::Accessor qw( id is_new manager );
11 my($class, $request, $manager) = @_;
13 my $id = $manager->state->extract($request);
15 my $store = $manager->store->fetch($id);
16 return $class->new( id => $id, _stash => $store, manager => $manager );
18 $id = $manager->state->generate($request);
19 return $class->new( id => $id, _stash => {}, manager => $manager, is_new => 1 );
24 my ($class, %params) = @_;
25 bless { %params } => $class;
36 my ($self, $key) = @_;
37 $self->{_stash}{$key};
41 my ($self, $key, $value) = @_;
42 $self->{_stash}{$key} = $value;
46 my ($self, $key) = @_;
47 delete $self->{_stash}{$key};
52 keys %{$self->{_stash}};
55 ## Lifecycle Management
60 $self->manager->store->cleanup( $self->id );
61 $self->manager->state->expire_session_id( $self->id );
65 my ($self, $response) = @_;
66 $self->manager->store->store( $self->id, $self );
67 $self->manager->state->finalize( $self->id, $response );
78 Plack::Session - Middleware for session management
84 my $store = Plack::Session::Store->new;
85 my $state = Plack::Session::State->new;
87 my $s = Plack::Session->new(
90 request => Plack::Request->new( $env )
97 This is the core session object, you probably want to look
98 at L<Plack::Middleware::Session>, unless you are writing your
99 own session middleware component.
105 =item B<new ( %params )>
107 The constructor expects keys in C<%params> for I<state>,
108 I<store> and I<request>. The I<request> param is expected to be
109 a L<Plack::Request> instance or an object with an equivalent
114 This is the accessor for the session id.
118 This is expected to be a L<Plack::Session::State> instance or
119 an object with an equivalent interface.
123 This is expected to be a L<Plack::Session::Store> instance or
124 an object with an equivalent interface.
128 =head2 Session Data Management
130 These methods allows you to read and write the session data like
131 Perl's normal hash. The operation is not synced to the storage until
132 you call C<finalize> on it.
136 =item B<get ( $key )>
138 =item B<set ( $key, $value )>
140 =item B<remove ( $key )>
146 =head2 Session Lifecycle Management
152 This method can be called to expire the current session id. It
153 will call the C<cleanup> method on the C<store> and the C<finalize>
154 method on the C<state>, passing both of them the session id and
157 =item B<finalize ( $response )>
159 This method should be called at the end of the response cycle. It
160 will call the C<store> method on the C<store> and the
161 C<expire_session_id> method on the C<state>, passing both of them
162 the session id. The C<$response> is expected to be a L<Plack::Response>
163 instance or an object with an equivalent interface.
169 All complex software has bugs lurking in it, and this module is no
170 exception. If you find a bug please either email me, or add the bug
175 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
177 =head1 COPYRIGHT AND LICENSE
179 Copyright 2009, 2010 Infinity Interactive, Inc.
181 L<http://www.iinteractive.com>
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.