| 1 | # | ||||||||||
| 2 | # This file is part of Jedi-Plugin-Session | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is copyright (c) 2013 by celogeek <me@celogeek.com>. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 8 | # | ||||||||||
| 9 | package Jedi::Plugin::Session::Backend::SQLite; | ||||||||||
| 10 | |||||||||||
| 11 | # ABSTRACT: Backend storage for SQLite | ||||||||||
| 12 | |||||||||||
| 13 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 3 | our $VERSION = '0.05'; # VERSION | |||||||||
| 16 | 3 | ▶ | use Time::Duration::Parse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 3 | ▶ | use Sereal qw/encode_sereal decode_sereal/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 3 | ▶ | use Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 3 | ▶ | use Jedi::Plugin::Session::Backend::SQLite::DB; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 3 | ▶ | use DBIx::Class::Migration; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 3 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | has 'database' => ( | ||||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | 2 | ▶ | coerce => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 2 | my ($db) = @_; | |||||||||
| 28 | 2 | my $dbfile = file($db); | |||||||||
| 29 | 2 | $dbfile->dir->mkpath; | |||||||||
| 30 | 2 | return _prepare_database($dbfile); | |||||||||
| 31 | } | ||||||||||
| 32 | 3 | ); | |||||||||
| 33 | |||||||||||
| 34 | has 'expires_in' => ( | ||||||||||
| 35 | is => 'ro', | ||||||||||
| 36 | 0 | ▶ | default => sub { 3 * 3600 }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 37 | 2 | ▶ | coerce => sub { parse_duration( $_[0] ) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 3 | ); | |||||||||
| 39 | |||||||||||
| 40 | ## no critic (NamingConventions::ProhibitAmbiguousNames) | ||||||||||
| 41 | |||||||||||
| 42 | 9 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 43 | 9 | my ( $self, $uuid ) = @_; | |||||||||
| 44 | 9 | ▶ | return if !defined $uuid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 9 | my $resultset = $self->database->resultset('Session'); | |||||||||
| 46 | 9 | my $now = time; | |||||||||
| 47 | 9 | my $data = $resultset->find($uuid); | |||||||||
| 48 | 9 | my $session; | |||||||||
| 49 | 9 | ▶ | if ( defined $data ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 5 | ▶ | if ( $data->expire_at > $now ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 4 | ▶ | return if !eval { $session = decode_sereal( $data->session ); 1 }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | } | ||||||||||
| 53 | else { | ||||||||||
| 54 | 1 | $resultset->search( { expire_at => { '<=' => $now } } ) | |||||||||
| 55 | ->delete_all; | ||||||||||
| 56 | } | ||||||||||
| 57 | } | ||||||||||
| 58 | 9 | return $session; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 4 | ▶ | sub set { | ||||||||
|
Subroutine
set: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 62 | 4 | my ( $self, $uuid, $value ) = @_; | |||||||||
| 63 | 4 | ▶ | return if !defined $uuid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 4 | my $session = encode_sereal($value); | |||||||||
| 65 | 4 | $self->database->resultset('Session')->update_or_create( | |||||||||
| 66 | { id => $uuid, | ||||||||||
| 67 | expire_at => time + $self->expires_in, | ||||||||||
| 68 | session => $session | ||||||||||
| 69 | } | ||||||||||
| 70 | ); | ||||||||||
| 71 | 4 | return 1; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | # PRIVATE | ||||||||||
| 75 | |||||||||||
| 76 | 2 | ▶ | sub _prepare_database { | ||||||||
|
Subroutine
_prepare_database: called CC=1 SCAR=0.0
| |||||||||||
| 77 | 2 | my ($dbfile) = @_; | |||||||||
| 78 | 2 | my @connect_info = ( "dbi:SQLite:dbname=" . $dbfile->stringify ); | |||||||||
| 79 | 2 | my $schema | |||||||||
| 80 | = Jedi::Plugin::Session::Backend::SQLite::DB->connect(@connect_info); | ||||||||||
| 81 | |||||||||||
| 82 | 2 | my $migration = DBIx::Class::Migration->new( schema => $schema, ); | |||||||||
| 83 | |||||||||||
| 84 | 2 | $migration->install_if_needed; | |||||||||
| 85 | 2 | $migration->upgrade; | |||||||||
| 86 | |||||||||||
| 87 | 2 | return $schema; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 3 | 1; | |||||||||
| 91 | |||||||||||
| 92 | __END__ | ||||||||||