line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MojoX::Session::Store::Redis; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
19840
|
use utf8; |
|
2
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
6
|
|
4
|
2
|
|
|
2
|
|
47
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
61
|
|
5
|
2
|
|
|
2
|
|
7
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
32
|
|
6
|
2
|
|
|
2
|
|
1117
|
use Redis; |
|
2
|
|
|
|
|
69799
|
|
|
2
|
|
|
|
|
50
|
|
7
|
2
|
|
|
2
|
|
620
|
use JSON; |
|
2
|
|
|
|
|
8258
|
|
|
2
|
|
|
|
|
10
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
211
|
use base 'MojoX::Session::Store'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
824
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
4705
|
use namespace::clean; |
|
2
|
|
|
|
|
18399
|
|
|
2
|
|
|
|
|
7
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
__PACKAGE__->attr('redis'); |
14
|
|
|
|
|
|
|
__PACKAGE__->attr('redis_prefix'); |
15
|
|
|
|
|
|
|
__PACKAGE__->attr('_redis_dbid'); |
16
|
|
|
|
|
|
|
__PACKAGE__->attr('auto_purge'); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=encoding utf8 |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 NAME |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
MojoX::Session::Store::Redis - RedisDB Store for MojoX::Session |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub new { |
31
|
0
|
|
|
0
|
1
|
|
my ($class, $param) = @_; |
32
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(); |
33
|
0
|
|
|
|
|
|
bless $self, $class; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
0
|
|
|
|
$param ||= {}; |
36
|
0
|
|
0
|
|
|
|
$self->_redis_dbid(delete $param->{redis_dbid} || 0); |
37
|
0
|
|
0
|
|
|
|
$self->redis_prefix(delete $param->{redis_prefix} || 'mojo-session'); |
38
|
0
|
|
0
|
|
|
|
$self->auto_purge( delete $param->{auto_purge} // 1 ); |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
0
|
|
|
|
$self->redis($param->{redis} || Redis->new(%$param)); |
41
|
0
|
|
|
|
|
|
$self->redis->select($self->_redis_dbid); |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
return $self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub create { |
48
|
0
|
|
|
0
|
1
|
|
my ($self, $sid, $expires, $data) = @_; |
49
|
0
|
|
|
|
|
|
my $prefix = $self->redis_prefix; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
$data = encode_json($data) if $data; |
52
|
0
|
|
|
|
|
|
$self->redis->hmset("$prefix:$sid", 'sid' => $sid, 'data' => $data, 'expires' => $expires); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# ttl |
55
|
0
|
0
|
0
|
|
|
|
if ( $self->auto_purge and $expires > 0 ) { |
56
|
0
|
|
|
|
|
|
$self->redis->expire("$prefix:$sid", $expires - time); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# FIXME |
60
|
|
|
|
|
|
|
# Check error |
61
|
|
|
|
|
|
|
#~ require Data::Dumper; |
62
|
|
|
|
|
|
|
#~ warn Data::Dumper::Dumper($err); |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub update { |
69
|
0
|
|
|
0
|
1
|
|
shift->create(@_); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub load { |
74
|
0
|
|
|
0
|
1
|
|
my ($self, $sid) = @_; |
75
|
0
|
|
|
|
|
|
my $prefix = $self->redis_prefix; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my %session = $self->redis->hgetall("$prefix:$sid"); |
78
|
0
|
0
|
|
|
|
|
$session{'data'} = $session{'data'} ? decode_json($session{'data'}) : undef ; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return ($session{'expires'}, $session{'data'}); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub delete { |
85
|
0
|
|
|
0
|
1
|
|
my ($self, $sid) = @_; |
86
|
0
|
|
|
|
|
|
my $prefix = $self->redis_prefix; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
$self->redis->del("$prefix:$sid"); |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
return 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub redis_dbid { |
95
|
0
|
|
|
0
|
1
|
|
my ($self, $dbid) = @_; |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
return $self->_redis_dbid unless defined $dbid; |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
$self->_redis_dbid($dbid); |
100
|
0
|
|
|
|
|
|
$self->redis->select( $self->_redis_dbid ); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 SYNOPSIS |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $session = MojoX::Session->new( |
107
|
|
|
|
|
|
|
tx => $tx, |
108
|
|
|
|
|
|
|
store => MojoX::Session::Store::Redis->new({ |
109
|
|
|
|
|
|
|
server => '127.0.0.1:6379', |
110
|
|
|
|
|
|
|
redis_prefix => 'mojo-session', |
111
|
|
|
|
|
|
|
redis_dbid => 0, |
112
|
|
|
|
|
|
|
}), |
113
|
|
|
|
|
|
|
transport => MojoX::Session::Transport::Cookie->new, |
114
|
|
|
|
|
|
|
); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# see doc for MojoX::Session |
117
|
|
|
|
|
|
|
# see doc for Redis |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
And later when you need to use it in Mojolicious Controller |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $session = $self->stash('mojox-session'); |
122
|
|
|
|
|
|
|
$session->load; |
123
|
|
|
|
|
|
|
$session->create unless $session->sid; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
#set |
126
|
|
|
|
|
|
|
$session->data( |
127
|
|
|
|
|
|
|
id => 5, |
128
|
|
|
|
|
|
|
name => 'hoge', |
129
|
|
|
|
|
|
|
); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
#get |
132
|
|
|
|
|
|
|
my $name = $session->data('name'); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 DESCRIPTION |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L is a store for L that stores a |
138
|
|
|
|
|
|
|
session in a L database. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
L implements the following attributes. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 C |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Get and set Redis object. See doc for L param. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
$store->redis( Redis->new($param) ); |
150
|
|
|
|
|
|
|
my $redis = $store->redis; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 C |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Get and set the Key prefix of the stored session in Redis. |
155
|
|
|
|
|
|
|
Default is 'mojo-session'. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
$store->redis_prefix('mojo-session'); |
158
|
|
|
|
|
|
|
my $prefix = $store->redis_prefix; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 C |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Get and set the DB ID Number to use in Redis DB. |
163
|
|
|
|
|
|
|
Default is 0. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
$store->redis_dbid(0); |
166
|
|
|
|
|
|
|
my $dbid = $store->redis_dbid; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 C |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Enable/disable auto purge. |
171
|
|
|
|
|
|
|
When enable, session object/data stored in RedisDB will be automatically purged after TTL. |
172
|
|
|
|
|
|
|
This is done by setting expire time for objects just right after creating them. |
173
|
|
|
|
|
|
|
Changing this can only affect on objects created/updated after the change. |
174
|
|
|
|
|
|
|
Default is 1 (enable). |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
$store->auto_purge(1); |
177
|
|
|
|
|
|
|
my $is_auto_purge_enabled = $store->auto_purge; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 METHODS |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
L inherits all methods from |
182
|
|
|
|
|
|
|
L, and few more. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 C |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
C uses the redis_prefix and redis_dbid parameters for the Key name prefix |
187
|
|
|
|
|
|
|
and the DB ID Number respectively. All other parameters are passed to Cnew()>. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 C |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Insert session to Redis. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 C |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Update session in Redis. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head2 C |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Load session from Redis. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 C |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Delete session from Redis. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 AUTHOR |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
BlueT - Matthew Lien - 練喆明, C<< >> |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 BUGS |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
214
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
215
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 CREDITS |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Tatsuya Fukata, L |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head1 CONTRIBUTE |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Main: |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
bzr repository etc at L. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
A copy of the codes: |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
git repository etc at L. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 SUPPORT |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
perldoc MojoX::Session::Store::Redis |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
You can also look for information at: |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=over 4 |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
L |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
L |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=item * CPAN Ratings |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
L |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=item * Search CPAN |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
L |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=back |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Copyright 2011 BlueT - Matthew Lien - 練喆明. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
271
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
272
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=cut |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
1; # End of MojoX::Session::Store::Redis |