-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redis.pm
129 lines (87 loc) · 2.52 KB
/
Redis.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package Dancer::Session::Redis;
#ABSTRACT: Redis backend for Dancer 2 session storage
use Moo;
with 'Dancer::Core::Role::SessionFactory';
use JSON;
use Redis;
use Dancer::Core::Types;
use Dancer::Core::Session;
=attr redis
Accessor to the Redis client.
=cut
has redis => (
is => 'rw',
isa => InstanceOf ['Redis'],
lazy => 1,
builder => '_build_redis',
);
sub _build_redis {
my ($self) = @_;
Redis->new(
server => $self->server,
password => $self->password,
encoding => undef,
);
}
=attr server
The server to connect to. Syntax is C<server:port>.
=cut
has server => (is => 'ro', required => 1);
=attr password
The password if any.
=cut
has password => (is => 'ro');
=attr key_prefix
A prefix for naming the keys used to store sessions.
Default is C<dancer_session_>.
=cut
has key_prefix => (
is => 'ro',
isa => Str,
default => sub { 'dancer_session_' },
);
# implementation of the role's interface
sub _retrieve {
my ($self, $session_id) = @_;
my $json = $self->redis->get($self->key_prefix.$session_id);
my $hash = from_json( $json );
return bless $hash, 'Dancer::Core::Session';
}
sub _flush {
my ($self, $session) = @_;
my $json = to_json( { %{ $session } } );
$self->redis->set($self->key_prefix.$session->id, $json);
}
sub _destroy {
my ($self, $session_id) = @_;
$self->redis->del($self->key_prefix.$session_id);
}
sub _sessions {
my ($self) = @_;
my @keys = $self->redis->keys($self->key_prefix.'*');
my $prefix = $self->key_prefix;
return [ map { s/$prefix//; $_ } @keys ];
}
1;
__END__
=head1 DISCLAIMER
This engine does not work with Dancer 1.
=head1 DESCRIPTION
This engine provides support for session storage in Redis for Dancer 2.
This module has been written during the Perl Dancer Advent Calendar 2012 effort,
for demonstrating how to write session engine for Dancer 2.
=head1 CONFIGURATION
The only bits of configuration that are needed are the C<server:port> pair to
connect to for storing session, and the optional password.
The best way to configure the engine is in your config file:
engines:
session:
Redis:
server: '127.0.0.1:6379'
password: 's3cr3t'
Now you should of course set to C<Redis> the C<session> setting:
session: 'Redis'
Nothing more is needed. Your sessions will be stored in Redis now.
=head1 AUTHOR
This module has been written by Alexis Sukrieh C<< <sukria@gmail.com> >>.
It's released under the same terms as Perl itself.