Commit | Line | Data |
02e67ead |
1 | package Plack::Session::Store::Cache; |
2 | use strict; |
3 | use warnings; |
4 | |
000c696e |
5 | our $VERSION = '0.03'; |
30cc0a71 |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8 | use Scalar::Util qw[ blessed ]; |
02e67ead |
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 { |
4a0cb5a0 |
27 | my ($self, $session_id ) = @_; |
28 | $self->cache->get($session_id); |
02e67ead |
29 | } |
30 | |
31 | sub store { |
4a0cb5a0 |
32 | my ($self, $session_id, $session) = @_; |
85265792 |
33 | $self->cache->set($session_id => $session); |
02e67ead |
34 | } |
35 | |
08b2e16d |
36 | sub remove { |
02e67ead |
37 | my ($self, $session_id) = @_; |
38 | $self->cache->remove($session_id); |
39 | } |
40 | |
41 | 1; |
42 | |
43 | __END__ |
44 | |
45 | =pod |
46 | |
47 | =head1 NAME |
48 | |
49 | Plack::Session::Store::Cache - Cache session store |
50 | |
51 | =head1 SYNOPSIS |
52 | |
53 | use Plack::Builder; |
54 | use Plack::Session::Store::Cache; |
55 | use CHI; |
56 | |
57 | my $app = sub { |
58 | return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ]; |
59 | }; |
60 | |
61 | builder { |
62 | enable 'Session', |
63 | store => Plack::Session::Store::Cache->new( |
64 | cache => CHI->new(driver => 'FastMmap') |
65 | ); |
66 | $app; |
67 | }; |
68 | |
69 | =head1 DESCRIPTION |
70 | |
7b024adb |
71 | This will persist session data using any module which implements the |
72 | L<Cache> interface. This offers a lot of flexibility due to the many |
73 | excellent L<Cache>, L<Cache::Cache> and L<CHI> drivers available. |
02e67ead |
74 | |
75 | This is a subclass of L<Plack::Session::Store> and implements |
76 | it's full interface. |
77 | |
78 | =head1 METHODS |
79 | |
80 | =over 4 |
81 | |
82 | =item B<new ( %params )> |
83 | |
7b024adb |
84 | The constructor expects the I<cache> param to be an object instance |
85 | which has the I<get>, I<set>, and I<remove> methods, it will throw an |
86 | exception if that is not the case. |
02e67ead |
87 | |
88 | =item B<cache> |
89 | |
7b024adb |
90 | A simple accessor for the cache handle. |
91 | |
02e67ead |
92 | =back |
93 | |
94 | =head1 BUGS |
95 | |
96 | All complex software has bugs lurking in it, and this module is no |
97 | exception. If you find a bug please either email me, or add the bug |
98 | to cpan-RT. |
99 | |
100 | =head1 AUTHOR |
101 | |
102 | Masahiro Chiba |
103 | |
104 | =cut |