90189507b95e6cce259a89e0ae1a95623fa28913
[catagits/Web-Session.git] / lib / Plack / Session / Store / Cache.pm
1 package Plack::Session::Store::Cache;
2 use strict;
3 use warnings;
4
5 our $VERSION   = '0.03';
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use Scalar::Util qw[ blessed ];
9
10 use parent 'Plack::Session::Store';
11
12 use Plack::Util::Accessor qw[ cache ];
13
14 sub 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
26 sub 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
33 sub 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
45 sub 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
54 sub cleanup {
55     my ($self, $session_id) = @_;
56     $self->cache->remove($session_id);
57 }
58
59 sub dump_session {
60     my ($self, $session_id) = @_;
61     $self->cache->get( $session_id ) || {};
62 }
63
64 1;
65
66 __END__
67
68 =pod
69
70 =head1 NAME
71
72 Plack::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
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.
97
98 This is a subclass of L<Plack::Session::Store> and implements
99 it's full interface.
100
101 =head1 METHODS
102
103 =over 4
104
105 =item B<new ( %params )>
106
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.
110
111 =item B<cache>
112
113 A simple accessor for the cache handle.
114
115 =back
116
117 =head1 BUGS
118
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
121 to cpan-RT.
122
123 =head1 AUTHOR
124
125 Masahiro Chiba
126
127 =cut