1 package Plack::Session::Store::File;
5 our $VERSION = '0.09_03';
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) = @_;
35 my $file_path = $self->_get_session_file_path( $session_id );
36 return unless -f $file_path;
38 $self->deserializer->( $file_path );
42 my ($self, $session_id, $session) = @_;
43 my $file_path = $self->_get_session_file_path( $session_id );
44 $self->serializer->( $session, $file_path );
48 my ($self, $session_id) = @_;
49 unlink $self->_get_session_file_path( $session_id );
52 sub _get_session_file_path {
53 my ($self, $session_id) = @_;
54 $self->dir . '/' . $session_id;
65 Plack::Session::Store::File - Basic file-based session store
70 use Plack::Middleware::Session;
71 use Plack::Session::Store::File;
74 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
79 store => Plack::Session::Store::File->new(
80 dir => '/path/to/sessions'
85 # with custom serializer/deserializer
89 store => Plack::Session::Store::File->new(
90 dir => '/path/to/sessions',
91 # YAML takes it's args the opposite order
92 serializer => sub { YAML::DumpFile( reverse @_ ) },
93 deserializer => sub { YAML::LoadFile( @_ ) },
100 This implements a basic file based storage for session data. By
101 default it will use L<Storable> to serialize and deserialize the
102 data, but this can be configured easily.
104 This is a subclass of L<Plack::Session::Store> and implements
111 =item B<new ( %params )>
113 The C<%params> can include I<dir>, I<serializer> and I<deserializer>
114 options. It will check to be sure that the I<dir> is writeable for
119 This is the directory to store the session data files in, if nothing
120 is provided then "/tmp" is used.
124 This is a CORE reference that implements the serialization logic.
125 The CODE ref gets two arguments, the C<$value>, which is a HASH
126 reference to be serialized, and the C<$file_path> to save it to.
127 It is not expected to return anything.
129 =item B<deserializer>
131 This is a CORE reference that implements the deserialization logic.
132 The CODE ref gets one argument, the C<$file_path> to load the data
133 from. It is expected to return a HASH reference.
139 All complex software has bugs lurking in it, and this module is no
140 exception. If you find a bug please either email me, or add the bug
145 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
147 =head1 COPYRIGHT AND LICENSE
149 Copyright 2009, 2010 Infinity Interactive, Inc.
151 L<http://www.iinteractive.com>
153 This library is free software; you can redistribute it and/or modify
154 it under the same terms as Perl itself.