Commit | Line | Data |
6f23813b |
1 | package Plack::Session::Store::CHI; |
2 | use strict; |
3 | use warnings; |
4 | |
6f23813b |
5 | use Scalar::Util qw/blessed/; |
6 | |
ac4892f4 |
7 | use parent 'Plack::Session::Store'; |
8 | |
9 | use Plack::Util::Accessor qw[ chi ]; |
10 | |
6f23813b |
11 | sub 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 | |
19 | sub 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 | |
26 | sub 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 | |
38 | sub 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 |
47 | sub cleanup { |
48 | my ($self, $session_id) = @_; |
49 | $self->chi->remove($session_id); |
50 | } |
51 | |
52 | 1; |
53 | |
54 | __END__ |
55 | |
43f34c01 |
56 | =pod |
57 | |
6f23813b |
58 | =head1 NAME |
59 | |
ac4892f4 |
60 | Plack::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 |
82 | This will persist session data using the L<CHI> module. This |
83 | offers a lot of flexibility due to the many excellent L<CHI> |
84 | drivers available. |
85 | |
43f34c01 |
86 | This is a subclass of L<Plack::Session::Store> and implements |
87 | it's full interface. |
ac4892f4 |
88 | |
43f34c01 |
89 | =head1 METHODS |
ac4892f4 |
90 | |
91 | =over 4 |
92 | |
43f34c01 |
93 | =item B<new ( %params )> |
ac4892f4 |
94 | |
43f34c01 |
95 | The constructor expects an the I<chi> param to be an |
96 | instance of L<CHI::Driver>, it will throw an exception |
97 | if that is not the case. |
ac4892f4 |
98 | |
43f34c01 |
99 | =item B<chi> |
ac4892f4 |
100 | |
101 | =back |
102 | |
43f34c01 |
103 | =head1 BUGS |
ac4892f4 |
104 | |
43f34c01 |
105 | All complex software has bugs lurking in it, and this module is no |
106 | exception. If you find a bug please either email me, or add the bug |
107 | to cpan-RT. |
6f23813b |
108 | |
109 | =head1 AUTHOR |
110 | |
111 | Masahiro Chiba |
112 | |
113 | =cut |