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