1 package Plack::Session::Store::Cache;
5 use Scalar::Util qw/blessed/;
7 use parent 'Plack::Session::Store';
9 use Plack::Util::Accessor qw[ cache ];
12 my ($class, %params) = @_;
14 die('cache require get, set and remove method.')
15 unless blessed $params{cache}
16 && $params{cache}->can('get')
17 && $params{cache}->can('set')
18 && $params{cache}->can('remove');
20 bless { %params } => $class;
24 my ($self, $session_id, $key) = @_;
25 my $cache = $self->cache->get($session_id);
27 return $cache->{ $key };
31 my ($self, $session_id, $key, $data) = @_;
32 my $cache = $self->cache->get($session_id);
34 $cache = {$key => $data};
37 $cache->{$key} = $data;
39 $self->cache->set($session_id => $cache);
43 my ($self, $session_id, $key) = @_;
44 my $cache = $self->cache->get($session_id);
45 return unless exists $cache->{$key};
47 delete $cache->{ $key };
48 $self->cache->set($session_id => $cache);
52 my ($self, $session_id) = @_;
53 $self->cache->remove($session_id);
57 my ($self, $session_id) = @_;
58 $self->cache->get( $session_id ) || {};
69 Plack::Session::Store::Cache - Cache session store
74 use Plack::Session::Store::Cache;
78 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
83 store => Plack::Session::Store::Cache->new(
84 cache => CHI->new(driver => 'FastMmap')
91 This will persist session data using any module which implements the
92 L<Cache> interface. This offers a lot of flexibility due to the many
93 excellent L<Cache>, L<Cache::Cache> and L<CHI> drivers available.
95 This is a subclass of L<Plack::Session::Store> and implements
102 =item B<new ( %params )>
104 The constructor expects the I<cache> param to be an object instance
105 which has the I<get>, I<set>, and I<remove> methods, it will throw an
106 exception if that is not the case.
110 A simple accessor for the cache handle.
116 All complex software has bugs lurking in it, and this module is no
117 exception. If you find a bug please either email me, or add the bug