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