| 1 | package MojoX::Session::Store::Redis; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Redis; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use base 'MojoX::Session::Store'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | __PACKAGE__->attr('redis'); | |||||||||||||||||||||||||||
| 14 | 2 | __PACKAGE__->attr('redis_prefix'); | |||||||||||||||||||||||||||
| 15 | 2 | __PACKAGE__->attr('_redis_dbid'); | |||||||||||||||||||||||||||
| 16 | 2 | __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 | 2 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 0 | my ($class, $param) = @_; | |||||||||||||||||||||||||||
| 32 | 0 | my $self = $class->SUPER::new(); | |||||||||||||||||||||||||||
| 33 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $param ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$param ||= {}
MC/DC0%0/1
$param ||= {}
$param
Truth table0%0/2
A
A= $param
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | $self->_redis_dbid(delete $param->{redis_dbid} || 0); | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $param->{'redis_dbid'} || 0
MC/DC0%0/1
delete $param->{'redis_dbid'} || 0
delete $param->{'redis_dbid'}
Truth table0%0/2
A
A= delete $param->{'redis_dbid'}
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $self->redis_prefix(delete $param->{redis_prefix} || 'mojo-session'); | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $param->{'redis_prefix'} || 'mojo-session'
MC/DC0%0/1
delete $param->{'redis_prefix'} || 'mojo-session'
delete $param->{'redis_prefix'}
Truth table0%0/2
A
A= delete $param->{'redis_prefix'}
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | $self->auto_purge( delete $param->{auto_purge} // 1 ); | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $param->{"auto_purge"} // 1
MC/DC0%0/1
delete $param->{"auto_purge"} // 1
delete $param->{"auto_purge"}
Truth table0%0/2
A
A= delete $param->{"auto_purge"}
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $self->redis($param->{redis} || Redis->new(%$param)); | ||||||||||||||||||||||||||
|
Condition0%0/3
$param->{'redis'} || 'Redis'->new(%$param)
MC/DC0%0/2
$param->{'redis'} || 'Redis'->new(%$param)
$param->{'redis'}
'Redis'->new(%$param)
Truth table0%0/3
A || B
A= $param->{'redis'}B= 'Redis'->new(%$param)
| |||||||||||||||||||||||||||||
| 41 | 0 | $self->redis->select($self->_redis_dbid); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | return $self; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub create { | ||||||||||||||||||||||||||
|
Subroutine
create: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 0 | my ($self, $sid, $expires, $data) = @_; | |||||||||||||||||||||||||||
| 49 | 0 | my $prefix = $self->redis_prefix; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $data = encode_json($data) if $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | $self->redis->hmset("$prefix:$sid", 'sid' => $sid, 'data' => $data, 'expires' => $expires); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # ttl | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( $self->auto_purge and $expires > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->auto_purge and $expires > 0
MC/DC0%0/2
$self->auto_purge and $expires > 0
$self->auto_purge
$expires > 0
Truth table0%0/3
A and B
A= $self->auto_purgeB= $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 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | shift->create(@_); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | my ($self, $sid) = @_; | |||||||||||||||||||||||||||
| 75 | 0 | my $prefix = $self->redis_prefix; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my %session = $self->redis->hgetall("$prefix:$sid"); | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | $session{'data'} = $session{'data'} ? decode_json($session{'data'}) : undef ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | return ($session{'expires'}, $session{'data'}); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | 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 | 0 | ▶ | sub redis_dbid { | ||||||||||||||||||||||||||
|
Subroutine
redis_dbid: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 0 | my ($self, $dbid) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return $self->_redis_dbid unless defined $dbid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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<MojoX::Session::Store::Redis> is a store for L<MojoX::Session> that stores a | ||||||||||||||||||||||||||||
| 138 | session in a L<Redis> database. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | L<MojoX::Session::Store::Redis> implements the following attributes. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head2 C<redis> | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Get and set Redis object. See doc for L<Redis> param. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | $store->redis( Redis->new($param) ); | ||||||||||||||||||||||||||||
| 150 | my $redis = $store->redis; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 C<redis_prefix> | ||||||||||||||||||||||||||||
| 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<redis_dbid> | ||||||||||||||||||||||||||||
| 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<auto_purge> | ||||||||||||||||||||||||||||
| 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<MojoX::Session::Store::Redis> inherits all methods from | ||||||||||||||||||||||||||||
| 182 | L<MojoX::Session::Store>, and few more. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head2 C<new> | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | C<new> 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 C<Redis->new()>. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 C<create> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Insert session to Redis. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 C<update> | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Update session in Redis. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 C<load> | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Load session from Redis. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head2 C<delete> | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Delete session from Redis. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | BlueT - Matthew Lien - 練喆明, C<< <BlueT at BlueT.org> >> | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 BUGS | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Please report any bugs or feature requests to C<bug-mojox-session-store-redis at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 214 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MojoX-Session-Store-Redis>. 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<https://github.com/fukata> | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 CONTRIBUTE | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Main: | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | bzr repository etc at L<https://launchpad.net/p5-mojox-session-store-redis>. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | A copy of the codes: | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | git repository etc at L<https://github.com/BlueT/p5-MojoX-Session-Store-Redis>. | ||||||||||||||||||||||||||||
| 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MojoX-Session-Store-Redis> | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | L<http://annocpan.org/dist/MojoX-Session-Store-Redis> | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | L<http://cpanratings.perl.org/d/MojoX-Session-Store-Redis> | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | L<http://search.cpan.org/dist/MojoX-Session-Store-Redis/> | ||||||||||||||||||||||||||||
| 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 | 2 | 1; # End of MojoX::Session::Store::Redis | |||||||||||||||||||||||||||