Commit | Line | Data |
f331b3e0 |
1 | package Plack::Session::Store::File; |
2 | use strict; |
3 | use warnings; |
4 | |
89d240fa |
5 | our $VERSION = '0.12'; |
30cc0a71 |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
f331b3e0 |
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 | |
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 | |
32 | sub 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 | |
41 | sub store { |
4a0cb5a0 |
42 | my ($self, $session_id, $session) = @_; |
caf3bd90 |
43 | my $file_path = $self->_get_session_file_path( $session_id ); |
85265792 |
44 | $self->serializer->( $session, $file_path ); |
f331b3e0 |
45 | } |
46 | |
08b2e16d |
47 | sub remove { |
f331b3e0 |
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 | |
f331b3e0 |
57 | 1; |
58 | |
59 | __END__ |
60 | |
61 | =pod |
62 | |
63 | =head1 NAME |
64 | |
65 | Plack::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 |
100 | This implements a basic file based storage for session data. By |
101 | default it will use L<Storable> to serialize and deserialize the |
000c696e |
102 | data, but this can be configured easily. |
3d92cf47 |
103 | |
104 | This is a subclass of L<Plack::Session::Store> and implements |
c4b2fb0e |
105 | its full interface. |
3d92cf47 |
106 | |
f331b3e0 |
107 | =head1 METHODS |
108 | |
109 | =over 4 |
110 | |
111 | =item B<new ( %params )> |
112 | |
3d92cf47 |
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 | |
f331b3e0 |
117 | =item B<dir> |
118 | |
3d92cf47 |
119 | This is the directory to store the session data files in, if nothing |
120 | is provided then "/tmp" is used. |
121 | |
f331b3e0 |
122 | =item B<serializer> |
123 | |
eb14af1e |
124 | This is a CODE reference that implements the serialization logic. |
3d92cf47 |
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 | |
f331b3e0 |
129 | =item B<deserializer> |
130 | |
eb14af1e |
131 | This is a CODE reference that implements the deserialization logic. |
3d92cf47 |
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 | |
f331b3e0 |
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 | |
000c696e |
149 | Copyright 2009, 2010 Infinity Interactive, Inc. |
f331b3e0 |
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 | |