1 package Plack::Session::Store::File;
7 use parent 'Plack::Session::Store';
9 use Plack::Util::Accessor qw[
16 my ($class, %params) = @_;
18 $params{'dir'} ||= '/tmp';
20 die "Storage directory (" . $params{'dir'} . ") is not writeable"
21 unless -w $params{'dir'};
23 $params{'serializer'} ||= sub { Storable::nstore( @_ ) };
24 $params{'deserializer'} ||= sub { Storable::retrieve( @_ ) };
26 bless { %params } => $class;
30 my ($self, $session_id, $key) = @_;
31 my $store = $self->_deserialize( $session_id );
32 return unless exists $store->{ $key };
33 return $store->{ $key };
37 my ($self, $session_id, $key, $data) = @_;
38 my $store = $self->_deserialize( $session_id );
39 $store->{ $key } = $data;
40 $self->_serialize( $session_id, $store );
44 my ($self, $session_id, $key) = @_;
45 my $store = $self->_deserialize( $session_id );
46 return unless exists $store->{ $key };
47 delete $store->{ $key };
48 $self->_serialize( $session_id, $store );
52 my ($self, $session_id) = @_;
53 unlink $self->_get_session_file_path( $session_id );
56 sub _get_session_file_path {
57 my ($self, $session_id) = @_;
58 $self->dir . '/' . $session_id;
62 my ($self, $session_id, $value) = @_;
63 my $file_path = $self->_get_session_file_path( $session_id );
64 $self->serializer->( $value, $file_path );
68 my ($self, $session_id) = @_;
69 my $file_path = $self->_get_session_file_path( $session_id );
70 $self->_serialize( $session_id, {} ) unless -f $file_path;
71 $self->deserializer->( $file_path );
83 Plack::Session::Store::File - Basic file-based session store
88 use Plack::Middleware::Session;
89 use Plack::Session::Store::File;
92 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
97 store => Plack::Session::Store::File->new(
98 dir => '/path/to/sessions'
103 # with custom serializer/deserializer
107 store => Plack::Session::Store::File->new(
108 dir => '/path/to/sessions',
109 # YAML takes it's args the opposite order
110 serializer => sub { YAML::DumpFile( reverse @_ ) },
111 deserializer => sub { YAML::LoadFile( @_ ) },
118 This implements a basic file based storage for session data. By
119 default it will use L<Storable> to serialize and deserialize the
120 data, but this can be configured easily.
122 This is a subclass of L<Plack::Session::Store> and implements
129 =item B<new ( %params )>
131 The C<%params> can include I<dir>, I<serializer> and I<deserializer>
132 options. It will check to be sure that the I<dir> is writeable for
137 This is the directory to store the session data files in, if nothing
138 is provided then "/tmp" is used.
142 This is a CORE reference that implements the serialization logic.
143 The CODE ref gets two arguments, the C<$value>, which is a HASH
144 reference to be serialized, and the C<$file_path> to save it to.
145 It is not expected to return anything.
147 =item B<deserializer>
149 This is a CORE reference that implements the deserialization logic.
150 The CODE ref gets one argument, the C<$file_path> to load the data
151 from. It is expected to return a HASH reference.
157 All complex software has bugs lurking in it, and this module is no
158 exception. If you find a bug please either email me, or add the bug
163 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
165 =head1 COPYRIGHT AND LICENSE
167 Copyright 2009 Infinity Interactive, Inc.
169 L<http://www.iinteractive.com>
171 This library is free software; you can redistribute it and/or modify
172 it under the same terms as Perl itself.