File Coverage

blib/lib/WWW/Session/Storage/Redis.pm
Criterion Covered Total %
statement 27 27 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 4 4 100.0
total 49 49 100.0


line stmt bran cond sub pod time code
1             package WWW::Session::Storage::Redis;
2              
3 4     4   553774 use 5.006;
  4         11  
4 4     4   16 use strict;
  4         5  
  4         96  
5 4     4   12 use warnings;
  4         15  
  4         207  
6              
7 4     4   17 use Carp qw(croak);
  4         5  
  4         333  
8              
9             my $HAS_CACHE_REDIS = eval { require Cache::Redis; 1 } ? 1 : 0;
10              
11             # Cache::Redis cannot store a key without an expiration time (an undef
12             # TTL falls back to its default_expires_in, 30 days), so "never expire"
13             # is approximated with a ten-year TTL.
14 4     4   26 use constant NEVER_EXPIRES_TTL => 60 * 60 * 24 * 365 * 10;
  4         4  
  4         1112  
15              
16             =head1 NAME
17              
18             WWW::Session::Storage::Redis - Redis storage for WWW::Session
19              
20             =head1 DESCRIPTION
21              
22             Redis backend for WWW::Session
23              
24             =head1 VERSION
25              
26             Version 0.04
27              
28             =cut
29              
30             our $VERSION = '0.04';
31              
32              
33             =head1 SYNOPSIS
34              
35             This module is used for storing serialized WWW::Session objects in redis
36              
37             Usage :
38              
39             use WWW::Session::Storage::Redis;
40              
41             my $storage = WWW::Session::Storage::Redis->new({ server => "127.0.0.1:6379" });
42             ...
43              
44             $storage->save($session_id,$expires,$serialized_data);
45              
46             my $serialized_data = $storage->retrieve($session_id);
47              
48             =head1 SUBROUTINES/METHODS
49              
50             =head2 new
51              
52             Creates a new WWW::Session::Storage::Redis object
53              
54             This method accepts only one argument, a hashref that contains all the options
55             that will be passed to the Cache::Redis module. The mandatory key in the hash
56             is "server" which must be a scalar containing the C server we want to
57             use.
58              
59             See Cache::Redis module for more details on available options
60              
61             Croaks if the Cache::Redis module is not installed or if the "server"
62             option is missing.
63              
64             Example :
65              
66             my $storage = WWW::Session::Storage::Redis->new({ server => "127.0.0.1:6379" });
67              
68             =cut
69             sub new {
70 9     9 1 142724 my $class = shift;
71 9         14 my $params = shift;
72              
73 9 100       172 croak "You must install the Cache::Redis module from CPAN to use the redis storage engine!"
74             unless $HAS_CACHE_REDIS;
75              
76             croak 'WWW::Session::Storage::Redis->new() requires a hashref of options containing a "server" key'
77 8 100 100     50 unless ref($params) eq 'HASH' && defined $params->{server};
78              
79 6         23 my $self = { options => $params,
80             redis => Cache::Redis->new(%$params),
81             };
82              
83 5         35 bless $self, $class;
84              
85 5         8 return $self;
86             }
87              
88             =head2 save
89              
90             Stores the session data in redis for the given number of seconds.
91              
92             An expiration time of -1 means the session should never expire. Redis
93             (through Cache::Redis) always stores keys with a TTL, so such sessions
94             are stored with a ten-year TTL instead.
95              
96             =cut
97             sub save {
98 4     4 1 15 my ($self,$sid,$expires,$string) = @_;
99              
100 4 100       14 return $self->{redis}->set($sid,$string,$expires == -1 ? NEVER_EXPIRES_TTL : $expires );
101             }
102              
103             =head2 retrieve
104              
105             Retrieves the session data for the given session id and returns the
106             string containing the serialized data. Returns undef if the session
107             does not exist or has expired (expiration is handled by redis itself).
108              
109             =cut
110             sub retrieve {
111 3     3 1 951 my ($self,$sid) = @_;
112              
113 3         7 return $self->{redis}->get($sid);
114             }
115              
116             =head2 delete
117              
118             Completely removes the session data for the given session id and
119             returns the result of the removal.
120              
121             =cut
122             sub delete {
123 1     1 1 10 my ($self,$sid) = @_;
124              
125 1         3 return $self->{redis}->remove($sid);
126             }
127              
128             =head1 AUTHORS
129              
130             Jeffrey Goff, C<< >>
131              
132             Horea Gligan, C<< >>
133              
134             =head1 BUGS
135              
136             Please report any bugs or feature requests to C, or through
137             the web interface at L. I will be notified, and then you'll
138             automatically be notified of progress on your bug as I make changes.
139              
140              
141              
142              
143             =head1 SUPPORT
144              
145             You can find documentation for this module with the perldoc command.
146              
147             perldoc WWW::Session::Storage::Redis
148              
149              
150             You can also look for information at:
151              
152             =over 4
153              
154             =item * RT: CPAN's request tracker (report bugs here)
155              
156             L
157              
158             =item * AnnoCPAN: Annotated CPAN documentation
159              
160             L
161              
162             =item * CPAN Ratings
163              
164             L
165              
166             =item * Search CPAN
167              
168             L
169              
170             =back
171              
172              
173             =head1 LICENSE
174              
175             This is free software; you can redistribute it and/or modify it under
176             the terms of:
177              
178             The GNU General Public License, Version 3, June 2007
179              
180             See L for the full license text.
181              
182              
183             =cut
184              
185             1; # End of WWW::Session::Storage::Redis