line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::Session::Store::Redis; |
2
|
1
|
|
|
1
|
|
12643
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
3
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
19
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: Redis Session store for Catalyst |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
|
|
456
|
use base qw/ |
8
|
|
|
|
|
|
|
Class::Data::Inheritable |
9
|
|
|
|
|
|
|
Catalyst::Plugin::Session::Store |
10
|
1
|
|
|
1
|
|
3
|
/; |
|
1
|
|
|
|
|
3
|
|
11
|
|
|
|
|
|
|
use MRO::Compat; |
12
|
|
|
|
|
|
|
use MIME::Base64 qw(encode_base64 decode_base64); |
13
|
|
|
|
|
|
|
use Redis; |
14
|
|
|
|
|
|
|
use Storable qw/nfreeze thaw/; |
15
|
|
|
|
|
|
|
use Try::Tiny; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata(qw/_session_redis_storage/); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub get_session_data { |
22
|
|
|
|
|
|
|
my ($c, $key) = @_; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$c->_verify_redis_connection; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
if(my ($sid) = $key =~ /^expires:(.*)/) { |
27
|
|
|
|
|
|
|
$c->log->debug("Getting expires key for $sid"); |
28
|
|
|
|
|
|
|
return $c->_session_redis_storage->get($key); |
29
|
|
|
|
|
|
|
} else { |
30
|
|
|
|
|
|
|
$c->log->debug("Getting $key"); |
31
|
|
|
|
|
|
|
my $data = $c->_session_redis_storage->get($key); |
32
|
|
|
|
|
|
|
if(defined($data)) { |
33
|
|
|
|
|
|
|
return thaw( decode_base64($data) ) |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
return; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub store_session_data { |
41
|
|
|
|
|
|
|
my ($c, $key, $data) = @_; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$c->_verify_redis_connection; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
if(my ($sid) = $key =~ /^expires:(.*)/) { |
46
|
|
|
|
|
|
|
$c->log->debug("Setting expires key for $sid: $data"); |
47
|
|
|
|
|
|
|
$c->_session_redis_storage->set($key, $data); |
48
|
|
|
|
|
|
|
} else { |
49
|
|
|
|
|
|
|
$c->log->debug("Setting $key"); |
50
|
|
|
|
|
|
|
$c->_session_redis_storage->set($key, encode_base64(nfreeze($data))); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# We use expire, not expireat because it's a 1.2 feature and as of this |
54
|
|
|
|
|
|
|
# release, 1.2 isn't done yet. |
55
|
|
|
|
|
|
|
my $exp = $c->session_expires; |
56
|
|
|
|
|
|
|
my $duration = $exp - time; |
57
|
|
|
|
|
|
|
$c->_session_redis_storage->expire($key, $duration); |
58
|
|
|
|
|
|
|
# $c->_session_redis_storage->expireat($key, $exp); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
return; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub delete_session_data { |
64
|
|
|
|
|
|
|
my ($c, $key) = @_; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$c->_verify_redis_connection; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
$c->log->debug("Deleting: $key"); |
69
|
|
|
|
|
|
|
$c->_session_redis_storage->del($key); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
return; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub delete_expired_sessions { |
75
|
|
|
|
|
|
|
my ($c) = @_; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Null op, Redis handles this for us! |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub setup_session { |
81
|
|
|
|
|
|
|
my ($c) = @_; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$c->maybe::next::method(@_); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _verify_redis_connection { |
87
|
|
|
|
|
|
|
my ($c) = @_; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my $cfg = $c->_session_plugin_config; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
try { |
92
|
|
|
|
|
|
|
$c->_session_redis_storage->ping; |
93
|
|
|
|
|
|
|
} catch { |
94
|
|
|
|
|
|
|
$c->_session_redis_storage( |
95
|
|
|
|
|
|
|
Redis->new( |
96
|
|
|
|
|
|
|
server => $cfg->{redis_server} || '127.0.0.1:6379', |
97
|
|
|
|
|
|
|
debug => $cfg->{redis_debug} || 0, |
98
|
|
|
|
|
|
|
reconnect => $cfg->{redis_reconnect} || 0 |
99
|
|
|
|
|
|
|
) |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
__END__ |