Reworked Session to make the expiration a store's responsibility and
[catagits/Web-Session.git] / lib / Plack / Session / Store / File.pm
CommitLineData
f331b3e0 1package Plack::Session::Store::File;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
f331b3e0 8use Storable ();
9
10use parent 'Plack::Session::Store';
11
12use Plack::Util::Accessor qw[
13 dir
14 serializer
15 deserializer
16];
17
18sub new {
19 my ($class, %params) = @_;
20
68fc9766 21 $params{'dir'} ||= $ENV{TMPDIR} || '/tmp';
f331b3e0 22
23 die "Storage directory (" . $params{'dir'} . ") is not writeable"
24 unless -w $params{'dir'};
25
3387a113 26 $params{'serializer'} ||= sub { Storable::lock_nstore( @_ ) };
27 $params{'deserializer'} ||= sub { Storable::lock_retrieve( @_ ) };
f331b3e0 28
9bb20750 29 bless { %params } => $class;
f331b3e0 30}
31
32sub fetch {
4a0cb5a0 33 my ($self, $session_id) = @_;
caf3bd90 34
35 my $file_path = $self->_get_session_file_path( $session_id );
36 return unless -f $file_path;
37
38 $self->deserializer->( $file_path );
f331b3e0 39}
40
41sub store {
4a0cb5a0 42 my ($self, $session_id, $session) = @_;
caf3bd90 43 my $file_path = $self->_get_session_file_path( $session_id );
44 $self->serializer->( $session->dump, $file_path );
f331b3e0 45}
46
47sub cleanup {
48 my ($self, $session_id) = @_;
49 unlink $self->_get_session_file_path( $session_id );
50}
51
52sub _get_session_file_path {
53 my ($self, $session_id) = @_;
54 $self->dir . '/' . $session_id;
55}
56
f331b3e0 571;
58
59__END__
60
61=pod
62
63=head1 NAME
64
65Plack::Session::Store::File - Basic file-based session store
66
3d92cf47 67=head1 SYNOPSIS
68
69 use Plack::Builder;
70 use Plack::Middleware::Session;
71 use Plack::Session::Store::File;
72
73 my $app = sub {
74 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
75 };
76
77 builder {
78 enable 'Session',
79 store => Plack::Session::Store::File->new(
80 dir => '/path/to/sessions'
81 );
82 $app;
83 };
84
85 # with custom serializer/deserializer
86
87 builder {
88 enable 'Session',
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( @_ ) },
94 );
95 $app;
96 };
97
f331b3e0 98=head1 DESCRIPTION
99
3d92cf47 100This implements a basic file based storage for session data. By
101default it will use L<Storable> to serialize and deserialize the
000c696e 102data, but this can be configured easily.
3d92cf47 103
104This is a subclass of L<Plack::Session::Store> and implements
105it's full interface.
106
f331b3e0 107=head1 METHODS
108
109=over 4
110
111=item B<new ( %params )>
112
3d92cf47 113The C<%params> can include I<dir>, I<serializer> and I<deserializer>
114options. It will check to be sure that the I<dir> is writeable for
115you.
116
f331b3e0 117=item B<dir>
118
3d92cf47 119This is the directory to store the session data files in, if nothing
120is provided then "/tmp" is used.
121
f331b3e0 122=item B<serializer>
123
3d92cf47 124This is a CORE reference that implements the serialization logic.
125The CODE ref gets two arguments, the C<$value>, which is a HASH
126reference to be serialized, and the C<$file_path> to save it to.
127It is not expected to return anything.
128
f331b3e0 129=item B<deserializer>
130
3d92cf47 131This is a CORE reference that implements the deserialization logic.
132The CODE ref gets one argument, the C<$file_path> to load the data
133from. It is expected to return a HASH reference.
134
f331b3e0 135=back
136
137=head1 BUGS
138
139All complex software has bugs lurking in it, and this module is no
140exception. If you find a bug please either email me, or add the bug
141to cpan-RT.
142
143=head1 AUTHOR
144
145Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
146
147=head1 COPYRIGHT AND LICENSE
148
000c696e 149Copyright 2009, 2010 Infinity Interactive, Inc.
f331b3e0 150
151L<http://www.iinteractive.com>
152
153This library is free software; you can redistribute it and/or modify
154it under the same terms as Perl itself.
155
156=cut
157