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 | |
56 | =head1 NAME |
57 | |
ac4892f4 |
58 | Plack::Session::Store::CHI - CHI session store |
6f23813b |
59 | |
60 | =head1 SYNOPSIS |
61 | |
62 | use Plack::Builder; |
63 | use Plack::Session::Store::CHI; |
64 | use CHI; |
65 | |
66 | my $app = sub { |
67 | return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ]; |
68 | }; |
69 | |
70 | builder { |
71 | enable 'Session', store => Plack::Session::Store::CHI->new(chi => CHI->new(driver => 'FastMmap')); |
72 | $app; |
73 | }; |
74 | |
ac4892f4 |
75 | =head1 METHODS |
76 | |
77 | =over 4 |
78 | |
79 | =item B<new ( %params )> |
80 | |
81 | =back |
82 | |
83 | =over 4 |
84 | |
85 | =item B<fetch ( $session_id, $key )> |
86 | |
87 | =item B<store ( $session_id, $key, $data )> |
88 | |
89 | =item B<delete ( $session_id, $key )> |
90 | |
91 | =back |
92 | |
93 | =over 4 |
94 | |
95 | =item B<cleanup ( $session_id )> |
96 | |
97 | =back |
6f23813b |
98 | |
99 | =head1 AUTHOR |
100 | |
101 | Masahiro Chiba |
102 | |
103 | =cut |