1 package Plack::Session::Store::Cache;
6 our $AUTHORITY = 'cpan:STEVAN';
8 use Scalar::Util qw[ blessed ];
10 use parent 'Plack::Session::Store';
12 use Plack::Util::Accessor qw[ cache ];
15 my ($class, %params) = @_;
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');
23 bless { %params } => $class;
27 my ($self, $session_id, $key) = @_;
28 my $cache = $self->cache->get($session_id);
30 return $cache->{ $key };
34 my ($self, $session_id, $key, $data) = @_;
35 my $cache = $self->cache->get($session_id);
37 $cache = {$key => $data};
40 $cache->{$key} = $data;
42 $self->cache->set($session_id => $cache);
46 my ($self, $session_id, $key) = @_;
47 my $cache = $self->cache->get($session_id);
48 return unless exists $cache->{$key};
50 delete $cache->{ $key };
51 $self->cache->set($session_id => $cache);
55 my ($self, $session_id) = @_;
56 $self->cache->remove($session_id);
60 my ($self, $session_id) = @_;
61 $self->cache->get( $session_id ) || {};
72 Plack::Session::Store::Cache - Cache session store
77 use Plack::Session::Store::Cache;
81 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
86 store => Plack::Session::Store::Cache->new(
87 cache => CHI->new(driver => 'FastMmap')
94 This will persist session data using any module which implements the
95 L<Cache> interface. This offers a lot of flexibility due to the many
96 excellent L<Cache>, L<Cache::Cache> and L<CHI> drivers available.
98 This is a subclass of L<Plack::Session::Store> and implements
105 =item B<new ( %params )>
107 The constructor expects the I<cache> param to be an object instance
108 which has the I<get>, I<set>, and I<remove> methods, it will throw an
109 exception if that is not the case.
113 A simple accessor for the cache handle.
119 All complex software has bugs lurking in it, and this module is no
120 exception. If you find a bug please either email me, or add the bug