Checking in changes prior to tagging of version 0.12.
[catagits/Web-Session.git] / lib / Plack / Session / Store / File.pm
1 package Plack::Session::Store::File;
2 use strict;
3 use warnings;
4
5 our $VERSION   = '0.12';
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use Storable ();
9
10 use parent 'Plack::Session::Store';
11
12 use Plack::Util::Accessor qw[
13     dir
14     serializer
15     deserializer
16 ];
17
18 sub new {
19     my ($class, %params) = @_;
20
21     $params{'dir'} ||= $ENV{TMPDIR} || '/tmp';
22
23     die "Storage directory (" . $params{'dir'} . ") is not writeable"
24         unless -w $params{'dir'};
25
26     $params{'serializer'}   ||= sub { Storable::lock_nstore( @_ ) };
27     $params{'deserializer'} ||= sub { Storable::lock_retrieve( @_ ) };
28
29     bless { %params } => $class;
30 }
31
32 sub fetch {
33     my ($self, $session_id) = @_;
34
35     my $file_path = $self->_get_session_file_path( $session_id );
36     return unless -f $file_path;
37
38     $self->deserializer->( $file_path );
39 }
40
41 sub store {
42     my ($self, $session_id, $session) = @_;
43     my $file_path = $self->_get_session_file_path( $session_id );
44     $self->serializer->( $session, $file_path );
45 }
46
47 sub remove {
48     my ($self, $session_id) = @_;
49     unlink $self->_get_session_file_path( $session_id );
50 }
51
52 sub _get_session_file_path {
53     my ($self, $session_id) = @_;
54     $self->dir . '/' . $session_id;
55 }
56
57 1;
58
59 __END__
60
61 =pod
62
63 =head1 NAME
64
65 Plack::Session::Store::File - Basic file-based session store
66
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
98 =head1 DESCRIPTION
99
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. 
103
104 This is a subclass of L<Plack::Session::Store> and implements
105 its full interface.
106
107 =head1 METHODS
108
109 =over 4
110
111 =item B<new ( %params )>
112
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
115 you.
116
117 =item B<dir>
118
119 This is the directory to store the session data files in, if nothing
120 is provided then "/tmp" is used.
121
122 =item B<serializer>
123
124 This is a CODE 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.
128
129 =item B<deserializer>
130
131 This is a CODE 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.
134
135 =back
136
137 =head1 BUGS
138
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
141 to cpan-RT.
142
143 =head1 AUTHOR
144
145 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
146
147 =head1 COPYRIGHT AND LICENSE
148
149 Copyright 2009, 2010 Infinity Interactive, Inc.
150
151 L<http://www.iinteractive.com>
152
153 This library is free software; you can redistribute it and/or modify
154 it under the same terms as Perl itself.
155
156 =cut
157