Huge rewrite of the code: $session->get, ->set etc. do not read from
[catagits/Web-Session.git] / lib / Plack / Session / Store / Cache.pm
CommitLineData
02e67ead 1package Plack::Session::Store::Cache;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
8use Scalar::Util qw[ blessed ];
02e67ead 9
10use parent 'Plack::Session::Store';
11
12use Plack::Util::Accessor qw[ cache ];
13
14sub new {
15 my ($class, %params) = @_;
16
17 die('cache require get, set and remove method.')
18 unless blessed $params{cache}
19 && $params{cache}->can('get')
20 && $params{cache}->can('set')
21 && $params{cache}->can('remove');
22
23 bless { %params } => $class;
24}
25
26sub fetch {
4a0cb5a0 27 my ($self, $session_id ) = @_;
28 $self->cache->get($session_id);
02e67ead 29}
30
31sub store {
4a0cb5a0 32 my ($self, $session_id, $session) = @_;
33 $self->cache->set($session_id => $session->dump);
02e67ead 34}
35
36sub cleanup {
37 my ($self, $session_id) = @_;
38 $self->cache->remove($session_id);
39}
40
411;
42
43__END__
44
45=pod
46
47=head1 NAME
48
49Plack::Session::Store::Cache - Cache session store
50
51=head1 SYNOPSIS
52
53 use Plack::Builder;
54 use Plack::Session::Store::Cache;
55 use CHI;
56
57 my $app = sub {
58 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
59 };
60
61 builder {
62 enable 'Session',
63 store => Plack::Session::Store::Cache->new(
64 cache => CHI->new(driver => 'FastMmap')
65 );
66 $app;
67 };
68
69=head1 DESCRIPTION
70
7b024adb 71This will persist session data using any module which implements the
72L<Cache> interface. This offers a lot of flexibility due to the many
73excellent L<Cache>, L<Cache::Cache> and L<CHI> drivers available.
02e67ead 74
75This is a subclass of L<Plack::Session::Store> and implements
76it's full interface.
77
78=head1 METHODS
79
80=over 4
81
82=item B<new ( %params )>
83
7b024adb 84The constructor expects the I<cache> param to be an object instance
85which has the I<get>, I<set>, and I<remove> methods, it will throw an
86exception if that is not the case.
02e67ead 87
88=item B<cache>
89
7b024adb 90A simple accessor for the cache handle.
91
02e67ead 92=back
93
94=head1 BUGS
95
96All complex software has bugs lurking in it, and this module is no
97exception. If you find a bug please either email me, or add the bug
98to cpan-RT.
99
100=head1 AUTHOR
101
102Masahiro Chiba
103
104=cut