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