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