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