1 package Plack::Session::Store::File;
6 our $AUTHORITY = 'cpan:STEVAN';
10 use parent 'Plack::Session::Store';
12 use Plack::Util::Accessor qw[
19 my ($class, %params) = @_;
21 $params{'dir'} ||= $ENV{TMPDIR} || '/tmp';
23 die "Storage directory (" . $params{'dir'} . ") is not writeable"
24 unless -w $params{'dir'};
26 $params{'serializer'} ||= sub { Storable::lock_nstore( @_ ) };
27 $params{'deserializer'} ||= sub { Storable::lock_retrieve( @_ ) };
29 bless { %params } => $class;
33 my ($self, $session_id) = @_;
34 return $self->_deserialize( $session_id );
38 my ($self, $session_id, $session) = @_;
39 $self->_serialize( $session_id, $session->dump );
43 my ($self, $session_id) = @_;
44 unlink $self->_get_session_file_path( $session_id );
47 sub _get_session_file_path {
48 my ($self, $session_id) = @_;
49 $self->dir . '/' . $session_id;
53 my ($self, $session_id, $value) = @_;
54 my $file_path = $self->_get_session_file_path( $session_id );
55 $self->serializer->( $value, $file_path );
59 my ($self, $session_id) = @_;
60 my $file_path = $self->_get_session_file_path( $session_id );
61 $self->_serialize( $session_id, {} ) unless -f $file_path;
62 $self->deserializer->( $file_path );
73 Plack::Session::Store::File - Basic file-based session store
78 use Plack::Middleware::Session;
79 use Plack::Session::Store::File;
82 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
87 store => Plack::Session::Store::File->new(
88 dir => '/path/to/sessions'
93 # with custom serializer/deserializer
97 store => Plack::Session::Store::File->new(
98 dir => '/path/to/sessions',
99 # YAML takes it's args the opposite order
100 serializer => sub { YAML::DumpFile( reverse @_ ) },
101 deserializer => sub { YAML::LoadFile( @_ ) },
108 This implements a basic file based storage for session data. By
109 default it will use L<Storable> to serialize and deserialize the
110 data, but this can be configured easily.
112 This is a subclass of L<Plack::Session::Store> and implements
119 =item B<new ( %params )>
121 The C<%params> can include I<dir>, I<serializer> and I<deserializer>
122 options. It will check to be sure that the I<dir> is writeable for
127 This is the directory to store the session data files in, if nothing
128 is provided then "/tmp" is used.
132 This is a CORE reference that implements the serialization logic.
133 The CODE ref gets two arguments, the C<$value>, which is a HASH
134 reference to be serialized, and the C<$file_path> to save it to.
135 It is not expected to return anything.
137 =item B<deserializer>
139 This is a CORE reference that implements the deserialization logic.
140 The CODE ref gets one argument, the C<$file_path> to load the data
141 from. It is expected to return a HASH reference.
147 All complex software has bugs lurking in it, and this module is no
148 exception. If you find a bug please either email me, or add the bug
153 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
155 =head1 COPYRIGHT AND LICENSE
157 Copyright 2009, 2010 Infinity Interactive, Inc.
159 L<http://www.iinteractive.com>
161 This library is free software; you can redistribute it and/or modify
162 it under the same terms as Perl itself.