Adding some additional docs and VERSION, AUTHORITY stuff
[catagits/Web-Session.git] / lib / Plack / Session / Store / Cache.pm
CommitLineData
02e67ead 1package Plack::Session::Store::Cache;
2use strict;
3use warnings;
4
30cc0a71 5our $VERSION = '0.01';
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 {
27 my ($self, $session_id, $key) = @_;
28 my $cache = $self->cache->get($session_id);
29 return unless $cache;
30 return $cache->{ $key };
31}
32
33sub store {
34 my ($self, $session_id, $key, $data) = @_;
35 my $cache = $self->cache->get($session_id);
36 if ( !$cache ) {
37 $cache = {$key => $data};
38 }
39 else {
40 $cache->{$key} = $data;
41 }
42 $self->cache->set($session_id => $cache);
43}
44
45sub delete {
46 my ($self, $session_id, $key) = @_;
47 my $cache = $self->cache->get($session_id);
48 return unless exists $cache->{$key};
49
50 delete $cache->{ $key };
51 $self->cache->set($session_id => $cache);
52}
53
54sub cleanup {
55 my ($self, $session_id) = @_;
56 $self->cache->remove($session_id);
57}
58
74e0eff0 59sub dump_session {
60 my ($self, $session_id) = @_;
61 $self->cache->get( $session_id ) || {};
62}
63
02e67ead 641;
65
66__END__
67
68=pod
69
70=head1 NAME
71
72Plack::Session::Store::Cache - Cache session store
73
74=head1 SYNOPSIS
75
76 use Plack::Builder;
77 use Plack::Session::Store::Cache;
78 use CHI;
79
80 my $app = sub {
81 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
82 };
83
84 builder {
85 enable 'Session',
86 store => Plack::Session::Store::Cache->new(
87 cache => CHI->new(driver => 'FastMmap')
88 );
89 $app;
90 };
91
92=head1 DESCRIPTION
93
7b024adb 94This will persist session data using any module which implements the
95L<Cache> interface. This offers a lot of flexibility due to the many
96excellent L<Cache>, L<Cache::Cache> and L<CHI> drivers available.
02e67ead 97
98This is a subclass of L<Plack::Session::Store> and implements
99it's full interface.
100
101=head1 METHODS
102
103=over 4
104
105=item B<new ( %params )>
106
7b024adb 107The constructor expects the I<cache> param to be an object instance
108which has the I<get>, I<set>, and I<remove> methods, it will throw an
109exception if that is not the case.
02e67ead 110
111=item B<cache>
112
7b024adb 113A simple accessor for the cache handle.
114
02e67ead 115=back
116
117=head1 BUGS
118
119All complex software has bugs lurking in it, and this module is no
120exception. If you find a bug please either email me, or add the bug
121to cpan-RT.
122
123=head1 AUTHOR
124
125Masahiro Chiba
126
127=cut