| 1 | package CHI::Driver::DBIC; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Params::Validate qw/:all/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | extends 'CHI::Driver'; | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | =head2 Attributes | ||
| 12 | |||
| 13 | =over | ||
| 14 | |||
| 15 | =item schema | ||
| 16 | |||
| 17 | The DBIx::Class schema | ||
| 18 | |||
| 19 | =item resultset | ||
| 20 | |||
| 21 | The DBIx::Class ResultSet which will be use to operate on the database table. | ||
| 22 | Internally the calls will be $self->schema->($self->resultset) etc. | ||
| 23 | |||
| 24 | =item column_map | ||
| 25 | |||
| 26 | A hash ref with the keys key, data and expires_in. Used to map to the table columns. | ||
| 27 | Defaults to: | ||
| 28 | |||
| 29 | { | ||
| 30 | key => 'id', | ||
| 31 | data => 'session_data', | ||
| 32 | expires_in => 'timestamp' | ||
| 33 | } | ||
| 34 | |||
| 35 | =item expiry_calc_in | ||
| 36 | |||
| 37 | =item expiry_calc_out | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | # has 'schema' => ( 'is' => 'ro', 'isa' => 'Object', 'required' => 1 ); | ||
| 44 | has 'resultset' => ( 'is' => 'ro', 'isa' => 'Object', 'required' => 1 ); | ||
| 45 | has 'expiry_calc_in' => ( | ||
| 46 | 'is' => 'ro', | ||
| 47 | 'isa' => 'CodeRef', | ||
| 48 | default => sub { | ||
| 49 | my $self = shift; | ||
| 50 | return sub { my $expiry = shift; $expiry += time(); } | ||
| 51 | }, | ||
| 52 | lazy => 1 | ||
| 53 | ); | ||
| 54 | has 'expiry_calc_out' => ( | ||
| 55 | 'is' => 'ro', | ||
| 56 | 'isa' => 'CodeRef', | ||
| 57 | default => sub { | ||
| 58 | return sub { my ( $self, $expiry ); return $expiry; } | ||
| 59 | }, | ||
| 60 | lazy => 1 | ||
| 61 | ); | ||
| 62 | |||
| 63 | has 'column_map' => ( | ||
| 64 | 'is' => 'ro', | ||
| 65 | 'isa' => 'HashRef', | ||
| 66 | default => sub { | ||
| 67 | return { | ||
| 68 | key => 'id', | ||
| 69 | data => 'session_data', | ||
| 70 | expires_in => 'expires' | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | ); | ||
| 74 | |||
| 75 | has '_rs' => ( | ||
| 76 | 'is' => 'ro', | ||
| 77 | 'isa' => 'Object', | ||
| 78 | 'lazy' => 1, | ||
| 79 | 'default' => sub { my $self = shift; $self->resultset; } | ||
| 80 | ); | ||
| 81 | |||
| 82 | =head1 NAME | ||
| 83 | |||
| 84 | CHI::Driver::DBIC - The great new CHI::Driver::DBIC! | ||
| 85 | |||
| 86 | =head1 VERSION | ||
| 87 | |||
| 88 | Version 0.001_002 | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | our $VERSION = '0.001_002'; | ||
| 93 | |||
| 94 | =head1 SYNOPSIS | ||
| 95 | |||
| 96 | Quick summary of what the module does. | ||
| 97 | |||
| 98 | Perhaps a little code snippet. | ||
| 99 | |||
| 100 | use CHI::Driver::DBIC; | ||
| 101 | |||
| 102 | my $foo = CHI::Driver::DBIC->new(); | ||
| 103 | ... | ||
| 104 | |||
| 105 | =head1 EXPORT | ||
| 106 | |||
| 107 | A list of functions that can be exported. You can delete this section | ||
| 108 | if you don't export anything, such as for a purely object-oriented module. | ||
| 109 | |||
| 110 | =head1 SUBROUTINES/METHODS | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | =head2 store | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub store { | ||
| 119 | my ( $self, $key, $data, $expires_in ) = validate_pos( @_, 1, 1, 1, 0 ); | ||
| 120 | my $cm = $self->column_map; | ||
| 121 | my $hr = { | ||
| 122 | $cm->{key} => $key, | ||
| 123 | $cm->{data} => $data | ||
| 124 | }; | ||
| 125 | |||
| 126 | $hr->{ $cm->{expires_in} } = $self->expiry_calc_in->($expires_in); | ||
| 127 | $self->_rs->update_or_create($hr); | ||
| 128 | return 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 fetch | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub fetch { | ||
| 136 | my ( $self, $key ) = validate_pos( @_, 1, 1 ); | ||
| 137 | |||
| 138 | my $id = $self->column_map->{key}; | ||
| 139 | my $result = $self->_rs->find( { $id => $key } ); | ||
| 140 | return $result->session_data if $result; | ||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | =head2 remove | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub remove { | ||
| 149 | my ( $self, $key ) = validate_pos( @_, 1, 1 ); | ||
| 150 | my $result = $self->_rs->find( { $self->column_map->{key} => $key } ); | ||
| 151 | $result->delete if $result; | ||
| 152 | return 1; | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 clear | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub clear { | ||
| 160 | my ($self) = validate_pos( @_, 1 ); | ||
| 161 | $self->_rs->search( {} )->delete; | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | =head1 AUTHOR | ||
| 166 | |||
| 167 | Motortrak Ltd, C<< <duncan.garland at motortrak.com> >> | ||
| 168 | |||
| 169 | =head1 BUGS | ||
| 170 | |||
| 171 | Please report any bugs or feature requests to C<bug-chi-driver-dbic at rt.cpan.org>, or through | ||
| 172 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CHI-Driver-DBIC>. I will be notified, and then you'll | ||
| 173 | automatically be notified of progress on your bug as I make changes. | ||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | =head1 SUPPORT | ||
| 179 | |||
| 180 | You can find documentation for this module with the perldoc command. | ||
| 181 | |||
| 182 | perldoc CHI::Driver::DBIC | ||
| 183 | |||
| 184 | |||
| 185 | You can also look for information at: | ||
| 186 | |||
| 187 | =over 4 | ||
| 188 | |||
| 189 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 190 | |||
| 191 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CHI-Driver-DBIC> | ||
| 192 | |||
| 193 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 194 | |||
| 195 | L<http://annocpan.org/dist/CHI-Driver-DBIC> | ||
| 196 | |||
| 197 | =item * CPAN Ratings | ||
| 198 | |||
| 199 | L<http://cpanratings.perl.org/d/CHI-Driver-DBIC> | ||
| 200 | |||
| 201 | =item * Search CPAN | ||
| 202 | |||
| 203 | L<http://search.cpan.org/dist/CHI-Driver-DBIC/> | ||
| 204 | |||
| 205 | =back | ||
| 206 | |||
| 207 | |||
| 208 | =head1 ACKNOWLEDGEMENTS | ||
| 209 | |||
| 210 | |||
| 211 | =head1 LICENSE AND COPYRIGHT | ||
| 212 | |||
| 213 | Copyright 2014 Motortrak Ltd. | ||
| 214 | |||
| 215 | This program is free software; you can redistribute it and/or modify it | ||
| 216 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 217 | copy of the full license at: | ||
| 218 | |||
| 219 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 220 | |||
| 221 | Any use, modification, and distribution of the Standard or Modified | ||
| 222 | Versions is governed by this Artistic License. By using, modifying or | ||
| 223 | distributing the Package, you accept this license. Do not use, modify, | ||
| 224 | or distribute the Package, if you do not accept this license. | ||
| 225 | |||
| 226 | If your Modified Version has been derived from a Modified Version made | ||
| 227 | by someone other than you, you are nevertheless required to ensure that | ||
| 228 | your Modified Version complies with the requirements of this license. | ||
| 229 | |||
| 230 | This license does not grant you the right to use any trademark, service | ||
| 231 | mark, tradename, or logo of the Copyright Holder. | ||
| 232 | |||
| 233 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 234 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 235 | otherwise transfer the Package with respect to any patent claims | ||
| 236 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 237 | Package. If you institute patent litigation (including a cross-claim or | ||
| 238 | counterclaim) against any party alleging that the Package constitutes | ||
| 239 | direct or contributory patent infringement, then this Artistic License | ||
| 240 | to you shall terminate on the date that such litigation is filed. | ||
| 241 | |||
| 242 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 243 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 244 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 245 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 246 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 247 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 248 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 249 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 250 | |||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | __PACKAGE__->meta->make_immutable; | ||
| 255 | |||
| 256 | 1; # End of CHI::Driver::DBIC | ||