File Coverage

blib/lib/Plack/Session/Store/Redis.pm
Criterion Covered Total %
statement 18 43 41.8
branch 0 6 0.0
condition 0 25 0.0
subroutine 6 14 42.8
pod 4 4 100.0
total 28 92 30.4


line stmt bran cond sub pod time code
1             package Plack::Session::Store::Redis;
2              
3 4     4   434109 use strict;
  4         11  
  4         142  
4 4     4   23 use warnings;
  4         8  
  4         122  
5 4     4   1264 use parent 'Plack::Session::Store';
  4         355  
  4         21  
6              
7 4     4   9993 use Redis;
  4         268577  
  4         153  
8 4     4   5037 use JSON;
  4         64623  
  4         28  
9              
10 4     4   754 use Plack::Util::Accessor qw/prefix redis_factory redis expires server serializer deserializer/;
  4         8  
  4         53  
11              
12             =head1 NAME
13              
14             Plack::Session::Store::Redis - Redis based session store for Plack apps.
15              
16             =head1 VERSION
17              
18             Version 0.05
19              
20             =cut
21              
22             our $VERSION = '0.05';
23              
24              
25             =head1 SYNOPSIS
26              
27             use Plack::Builder;
28             use Plack::Middleware::Session;
29             use Plack::Session::Store::Redis;
30              
31             my $app = sub { ... };
32              
33             builder {
34             enable 'Session', store => 'Redis';
35             $app;
36             };
37              
38             =head1 DESCRIPTION
39              
40             This module will store Plack session data on a redis server. NOTE:
41             only works with redis 1.2.x, which appears to be a limitation of
42             Redis.pm.
43              
44             =head1 METHODS
45              
46             =head2 new( %params )
47              
48             Create a instance of this module. No parameters are required, but
49             there are a few defaults that can be changed. You can set the IP
50             address of the server with the 'host' option, and the port with
51             'port'. By default all of the keys in Redis will be prefixed with
52             "session", but this can be changed with the 'prefix' option. You
53             can also provide an 'expires' option that will be used to set an
54             expiration on the redis key.
55              
56             =cut
57              
58             sub new {
59 0     0 1   my ($class, %params) = @_;
60              
61 0   0       my $server = $ENV{REDIS_SERVER} ||
62             ($params{host} || '127.0.0.1').":".
63             ($params{port} || 6379);
64              
65 0   0 0     my $redis_factory = $params{redis_factory} || sub { Redis->new(server => $server); };
  0            
66             my $self = {
67             prefix => $params{prefix} || 'session',
68             redis => $params{redis} || $redis_factory->(),
69             redis_factory => $redis_factory,
70             server => $params{server} || $server,
71             expires => $params{expires} || undef,
72 0     0     serializer => $params{serializer} || sub { encode_json($_[0]); },
73 0     0     deserializer => $params{deserializer} || sub { decode_json($_[0]); }
74 0   0       };
      0        
      0        
      0        
      0        
      0        
75              
76 0           bless $self, $class;
77             }
78              
79             =head2 fetch( $session_id )
80              
81             Fetches a session object from the database.
82              
83             =cut
84              
85             sub fetch {
86 0     0 1   my ($self, $session_id) = @_;
87              
88 0           my $session = $self->_exec("get", $session_id);
89              
90 0 0         return $session ? $self->{deserializer}($session) : ();
91             }
92              
93             sub _exec {
94 0     0     my ($self, $command, $session, @args) = @_;
95 0           unshift @args, $self->prefix."_".$session;
96              
97 0           my $ret = eval {$self->redis->$command(@args)};
  0            
98              
99 0 0         if ($@) {
100 0           $self->redis($self->redis_factory->());
101 0           $ret = $self->redis->$command(@args);
102             }
103              
104 0 0 0       if ($self->expires and ($command eq "get" or $command eq "set")) {
      0        
105 0           $self->redis->expire($args[0], $self->expires);
106             }
107              
108 0           return $ret;
109             }
110              
111             =head2 store( $session_id, \%session_obj )
112              
113             Stores a session object in the database.
114              
115             =cut
116              
117             sub store {
118 0     0 1   my ($self, $session_id, $session_obj) = @_;
119              
120 0           $self->_exec("set", $session_id, $self->{serializer}($session_obj));
121             }
122              
123             =head2 remove( $session_id )
124              
125             Removes the session object from the database.
126              
127             =cut
128              
129             sub remove {
130 0     0 1   my ($self, $session_id) = @_;
131              
132 0           $self->_exec("del", $session_id);
133             }
134              
135             =head1 AUTHOR
136              
137             Lee Aylward, C<< >>
138              
139             =head1 BUGS
140              
141             Please report any bugs or feature requests to
142             C, or through the web
143             interface at
144             L.
145             I will be notified, and then you'll automatically be notified of
146             progress on your bug as I make changes.
147              
148              
149             =head1 SUPPORT
150              
151             You can find documentation for this module with the perldoc command.
152              
153             perldoc Plack::Session::Store::Redis
154              
155              
156             You can also look for information at:
157              
158             =over 4
159              
160             =item * RT: CPAN's request tracker
161              
162             L
163              
164             =item * AnnoCPAN: Annotated CPAN documentation
165              
166             L
167              
168             =item * CPAN Ratings
169              
170             L
171              
172             =item * Search CPAN
173              
174             L
175              
176             =back
177              
178              
179             =head1 ACKNOWLEDGEMENTS
180              
181              
182             =head1 LICENSE AND COPYRIGHT
183              
184             Copyright 2010 Lee Aylward.
185              
186             This program is free software; you can redistribute it and/or modify it
187             under the terms of either: the GNU General Public License as published
188             by the Free Software Foundation; or the Artistic License.
189              
190             See http://dev.perl.org/licenses/ for more information.
191              
192              
193             =cut
194              
195             1; # End of Plack::Session::Store::Redis