line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::Session::Store::Redis::Fast; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '1.001'; # VERSION |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
71503
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
36
|
|
6
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
|
|
639
|
use base qw/ |
9
|
|
|
|
|
|
|
Class::Data::Inheritable |
10
|
|
|
|
|
|
|
Catalyst::Plugin::Session::Store |
11
|
1
|
|
|
1
|
|
7
|
/; |
|
1
|
|
|
|
|
2
|
|
12
|
1
|
|
|
1
|
|
1633
|
use MRO::Compat; |
|
1
|
|
|
|
|
1858
|
|
|
1
|
|
|
|
|
38
|
|
13
|
1
|
|
|
1
|
|
548
|
use MIME::Base64 qw(encode_base64 decode_base64); |
|
1
|
|
|
|
|
708
|
|
|
1
|
|
|
|
|
67
|
|
14
|
1
|
|
|
1
|
|
718
|
use Storable qw/nfreeze thaw/; |
|
1
|
|
|
|
|
3306
|
|
|
1
|
|
|
|
|
133
|
|
15
|
1
|
|
|
1
|
|
587
|
use Try::Tiny; |
|
1
|
|
|
|
|
2070
|
|
|
1
|
|
|
|
|
61
|
|
16
|
1
|
|
|
1
|
|
574
|
use Redis::Fast; |
|
1
|
|
|
|
|
17762
|
|
|
1
|
|
|
|
|
653
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata(qw/_redis_connection/); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub get_session_data { |
21
|
0
|
|
|
0
|
1
|
|
my ($c, $key) = @_; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
$c->_verify_redis_connection; |
24
|
|
|
|
|
|
|
|
25
|
0
|
0
|
|
|
|
|
if(my ($sid) = $key =~ /^expires:(.*)/) { |
26
|
0
|
|
|
|
|
|
$c->log->debug("Getting expires key for $sid"); |
27
|
0
|
|
|
|
|
|
return $c->_redis_connection->get($key); |
28
|
|
|
|
|
|
|
} else { |
29
|
0
|
|
|
|
|
|
$c->log->debug("Getting $key"); |
30
|
0
|
|
|
|
|
|
my $data = $c->_redis_connection->get($key); |
31
|
0
|
0
|
|
|
|
|
if(defined($data)) { |
32
|
0
|
|
|
|
|
|
return thaw( decode_base64($data) ) |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
return; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub store_session_data { |
40
|
0
|
|
|
0
|
1
|
|
my ($c, $key, $data) = @_; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
$c->_verify_redis_connection; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $time = int($c->session_expires - time); |
45
|
0
|
0
|
|
|
|
|
if($time == 0) { |
46
|
0
|
|
|
|
|
|
$c->log->warn("skipping $key already expired at " . $c->session_expires ); |
47
|
|
|
|
|
|
|
} else { |
48
|
0
|
0
|
|
|
|
|
if(my ($sid) = $key =~ /^expires:(.*)/) { |
49
|
0
|
|
|
|
|
|
$c->log->debug("Setting expires key for '$sid: $data' expiry seconds ($time)"); |
50
|
0
|
|
|
|
|
|
$c->_redis_connection->setex($key, $time, $data); |
51
|
|
|
|
|
|
|
} else { |
52
|
0
|
|
|
|
|
|
$c->log->debug("Setting key '$key' with expiry seconds ($time)"); |
53
|
0
|
|
|
|
|
|
$c->_redis_connection->setex($key, $time, encode_base64(nfreeze($data))); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
return; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub delete_session_data { |
60
|
0
|
|
|
0
|
1
|
|
my ($c, $key) = @_; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
$c->_verify_redis_connection; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
$c->log->debug("Deleting: $key"); |
65
|
0
|
|
|
|
|
|
$c->_redis_connection->del($key); |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
return; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
0
|
1
|
|
sub delete_expired_sessions { |
71
|
|
|
|
|
|
|
# my ($c) = @_; |
72
|
|
|
|
|
|
|
#redis will delete |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub setup_session { |
76
|
0
|
|
|
0
|
0
|
|
my ($c) = @_; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
$c->maybe::next::method(@_); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _verify_redis_connection { |
82
|
0
|
|
|
0
|
|
|
my ($c) = @_; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my $cfg = $c->_session_plugin_config; |
85
|
0
|
|
0
|
|
|
|
$cfg->{server} //= '127.0.0.1:6379'; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
try { |
88
|
0
|
|
|
0
|
|
|
$c->_redis_connection->ping; |
89
|
|
|
|
|
|
|
} catch { |
90
|
0
|
|
|
0
|
|
|
$c->_redis_connection(Redis::Fast->new( %$cfg )); |
91
|
0
|
0
|
|
|
|
|
if (defined $cfg->{select_db}) { |
92
|
0
|
|
|
|
|
|
$c->_redis_connection->select($cfg->{select_db}); |
93
|
|
|
|
|
|
|
} |
94
|
0
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; # End of Catalyst::Plugin::Session::Store::Redis::Fast |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=pod |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=encoding UTF-8 |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 NAME |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Catalyst::Plugin::Session::Store::Redis::Fast - Catalyst session store Redis Fast Plugin |
108
|
|
|
|
|
|
|
works with redis 2.0.0 and above |
109
|
|
|
|
|
|
|
Redis::Fast is 50% faster than Redis.pm |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 VERSION |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Version 1.000 |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 SYNOPSIS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
use Catalyst qw/ |
118
|
|
|
|
|
|
|
Session |
119
|
|
|
|
|
|
|
Session::Store::Redis::Fast |
120
|
|
|
|
|
|
|
Session::State::Foo |
121
|
|
|
|
|
|
|
/; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
MyApp->config->{Plugin::Session} = { |
124
|
|
|
|
|
|
|
server => '127.0.0.1:6379', # Defaults to $ENV{REDIS_SERVER} or 127.0.0.1:6379 |
125
|
|
|
|
|
|
|
sock => '/path/to/socket', #(optional) use unix socket |
126
|
|
|
|
|
|
|
reconnect => 60, #(optional) Enable auto-reconnect |
127
|
|
|
|
|
|
|
every => 500_000, #(optional) Try to reconnect every 500ms up to 60 seconds until success |
128
|
|
|
|
|
|
|
encoding => undef, #(optional) Disable the automatic utf8 encoding => much more performance |
129
|
|
|
|
|
|
|
password => 'TEST', #(optional) default undef |
130
|
|
|
|
|
|
|
select_db => 4, #(optional) will use the redis db default 0 |
131
|
|
|
|
|
|
|
}; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# ... in an action: |
134
|
|
|
|
|
|
|
$c->session->{foo} = 'bar'; # will be saved |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 method |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 server (optional) either server or sock |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Defaults to $ENV{REDIS_SERVER} or 127.0.0.1:6379 |
141
|
|
|
|
|
|
|
MyApp->config->{Plugin::Session} = { |
142
|
|
|
|
|
|
|
server => '127.0.0.1:6379', |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 sock (optional) |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# use unix socket |
148
|
|
|
|
|
|
|
MyApp->config->{Plugin::Session} = { |
149
|
|
|
|
|
|
|
sock => '/path/to/socket', |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 reconnect & every (optional) |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
## Enable auto-reconnect |
155
|
|
|
|
|
|
|
## Try to reconnect every 500ms up to 60 seconds until success |
156
|
|
|
|
|
|
|
## Die if you can't after that |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
MyApp->config->{Plugin::Session} = { |
159
|
|
|
|
|
|
|
server => '127.0.0.1:6379', |
160
|
|
|
|
|
|
|
reconnect => 60, |
161
|
|
|
|
|
|
|
every => 500_000, |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 name (optional) |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
## Set the connection name (requires Redis 2.6.9) |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
MyApp->config->{Plugin::Session} = { |
169
|
|
|
|
|
|
|
server => '127.0.0.1:6379', |
170
|
|
|
|
|
|
|
name => 'TEST', |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 encoding (optional) |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
## Disable the automatic utf8 encoding => much more performance |
176
|
|
|
|
|
|
|
## !!!! This will be the default after redis version 2.000 |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
MyApp->config->{Plugin::Session} = { |
179
|
|
|
|
|
|
|
server => '127.0.0.1:6379', |
180
|
|
|
|
|
|
|
enci => 'TEST', |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 password (optional) |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
## Sets the connection password |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
MyApp->config->{Plugin::Session} = { |
188
|
|
|
|
|
|
|
server => '127.0.0.1:6379', |
189
|
|
|
|
|
|
|
encoding => undef |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 select_db (optional) |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
## Selects db to be used for connection |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
MyApp->config->{Plugin::Session} = { |
197
|
|
|
|
|
|
|
server => '127.0.0.1:6379', |
198
|
|
|
|
|
|
|
select_db => 3 |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 refer Redis::Fast for more connection method support |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
no_auto_connect_on_new => 1, |
204
|
|
|
|
|
|
|
server => '127.0.0.1:6379', |
205
|
|
|
|
|
|
|
write_timeout => 0.2, |
206
|
|
|
|
|
|
|
read_timeout => 0.2, |
207
|
|
|
|
|
|
|
cnx_timeout => 0.2, |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 AUTHOR |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Sushrut Pajai, C<< <spajai at cpan.org> >> |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 BUGS |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
C<bug-catalyst-plugin-session-store-redis-fast at rt.cpan.org>, or through |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-Session-Store-Redis-Fast>. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=cut |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head1 SUPPORT |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
perldoc Catalyst::Plugin::Session::Store::Redis::Fast |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
You can also look for information at: |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=over 6 |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-Session-Store-Redis-Fast> |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Catalyst-Plugin-Session-Store-Redis-Fast> |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item * CPAN Ratings |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
L<https://cpanratings.perl.org/d/Catalyst-Plugin-Session-Store-Redis-Fast> |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item * Search CPAN |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
L<https://metacpan.org/release/Catalyst-Plugin-Session-Store-Redis-Fast> |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item * github source code |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
L<https://github.com/spajai/Catalyst-Plugin-Session-Store-Redis-Fast> |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=item * pull request |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
L<https://github.com/spajai/Catalyst-Plugin-Session-Store-Redis-Fast/pulls> |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=back |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
This software is copyright (c) 2021 by Sushrut Pajai |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language itself. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=cut |