1 package Catalyst::Plugin::Session::State::Stash;
2 use base qw/Catalyst::Plugin::Session::State Class::Accessor::Fast/;
8 use Data::Dumper qw/Dumper/;
11 BEGIN { __PACKAGE__->mk_accessors(qw/_deleted_session_id _prepared/) }
13 sub _stash_key_components {
15 return ($c->config->{session}->{stash_delim}) ?
16 split $c->config->{session}->{stash_delim}, $c->config->{session}->{stash_key} :
17 $c->config->{session}->{stash_key};
22 # This turns the list of path components into a nested tree of hashrefs for obtaining info/storing in: 123/456 = {123}->{456}
24 $ref = ($ref->{$_} ||= {}) foreach $c->_stash_key_components;
29 my ( $c,$key,$value) = @_;
31 $c->_get_session->{$key} = $value;
37 $c->config->{session}->{stash_key}
43 my $id = $c->get_session_id;
48 $c->maybe::next::method( @_ );
53 if(!$c->_deleted_session_id and my $session = $c->_get_session) {
54 my $sid = $session->{id};
57 $c->maybe::next::method(@_);
62 $c->_set_session(id => $sid);
63 $c->maybe::next::method($sid);
66 sub get_session_expires {
68 my $session = $c->_get_session;
69 defined $session->{expires} ? $session->{expires} : undef;
72 sub set_session_expires {
73 my ( $c, $expires ) = @_;
75 $c->_set_session(expires => time() + $expires);
76 $c->maybe::next::method($expires)
79 sub delete_session_id {
81 $c->_deleted_session_id(1);
83 %{$c->_get_session} = ();
84 $c->maybe::next::method($sid);
95 Catalyst::Plugin::Session::State::Stash - Maintain session IDs using the stash
99 use Catalyst qw/Session Session::State::Stash Session::Store::Foo/;
103 An alternative state storage plugin that allows you some more flexibility in
104 dealing with session storage. This plugin loads and saves the session ID from
111 =item delete_session_id
113 Deletes the session. Unfortunately I've been unable to squash a bug that will
114 stop you from opening a new session in the same execution, however.
119 Gets the current session id.
123 Sets the session id to the C<shift>.
125 =item get_session_expires
127 Gets when the current session expires.
129 =item set_session_expires
131 Sets how many seconds from now the session should expire.
135 =head1 EXTENDED METHODS
141 Loads the id off the stash.
145 Defaults the C<stash_key> parameter to C<_session>.
155 The name of the hash key to use. Defaults to C<_session>.
159 If present, splits stash_key at this character to nest. E.g. delim of '/'
160 and key of '123/456' will store it as $c->stash->{123}->{456}
164 How long the session should last in seconds.
168 For example, you could stick this in MyApp.pm:
170 __PACKAGE__->config( session => {
171 stash_key => 'session_id',
176 You can't delete a session then create a new one. If this is important to you,
177 patches welcome. It is not important to me and fixing this for completeness
178 is pretty low on my list of priorities.
182 Manual work may be involved to make better use of this.
184 If you are writing a stateful web service with
185 L<Catalyst::Plugin::Server::XMLRPC>, you will probably only have to deal with
186 loading, as when saving, the ID will already be on the stash.
190 L<Catalyst>, L<Catalyst::Plugin::Session>, L<Catalyst::Plugin::Session::State>,
191 L<Catalyst::Plugin::Session::State::Cookie> (what you probably want).
195 James Laver E<lt>perl -e 'printf qw/%s@%s.com cpan jameslaver/'E<gt>
199 This module is derived from L<Catalyst::Plugin::Session::State::Cookie> code.
201 Thanks to anyone who wrote code for that.
203 Thanks to Kent Fredric for a patch for nested keys
207 This program is free software, you can redistribute it and/or modify it
208 under the same terms as Perl itself.