| 1 | package Mojolicious::Sessions::ThreeS::Storage::CHI; | ||||||||||
| 2 | 3 | $Mojolicious::Sessions::ThreeS::Storage::CHI::VERSION = '0.004'; | |||||||||
| 3 | 3 | ▶ | use Mojo::Base qw/Mojolicious::Sessions::ThreeS::Storage/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Mojolicious::Sessions::ThreeS::Storage::CHI - An adapter to store sessions in a CHI instance. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||
| 12 | |||||||||||
| 13 | my $storage = Mojolicious::Sessions::ThreeS::Storage::CHI->new({ chi => .. a CHI instance .. }); | ||||||||||
| 14 | |||||||||||
| 15 | # Use $storage in the Mojolicious::Sessions::ThreeS instance (or through the plugin): | ||||||||||
| 16 | |||||||||||
| 17 | $app->sessions( Mojolicious::Sessions::ThreeS->new({ storage => $storage , state => ... } ); | ||||||||||
| 18 | |||||||||||
| 19 | Note that you WILL have to depend on CHI and on JSON in your application to use this storage. | ||||||||||
| 20 | |||||||||||
| 21 | This distribution does not add these to the runtime dependencies to avoid clutter. | ||||||||||
| 22 | |||||||||||
| 23 | =head1 ATTRIBUTES | ||||||||||
| 24 | |||||||||||
| 25 | =head2 default_expiration | ||||||||||
| 26 | |||||||||||
| 27 | Default expiration length (in seconds) in case you dont define a default session expiration | ||||||||||
| 28 | at higher level. This is required to avoid CHI cache overflowing. | ||||||||||
| 29 | |||||||||||
| 30 | Defaults to 43200 (12 hours). | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | 3 | has 'default_expiration' => 43200; | |||||||||
| 35 | 3 | has 'chi'; | |||||||||
| 36 | 2 | ▶ | has 'json' => sub{ | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 2 | my $json = JSON->new(); | |||||||||
| 38 | 2 | $json->ascii( 1 ); | |||||||||
| 39 | # Encode stuff in ascii so there is no risk | ||||||||||
| 40 | # of bad decoding. | ||||||||||
| 41 | 2 | return $json; | |||||||||
| 42 | 3 | }; | |||||||||
| 43 | |||||||||||
| 44 | =head1 METHODS | ||||||||||
| 45 | |||||||||||
| 46 | =head2 get_session | ||||||||||
| 47 | |||||||||||
| 48 | See L<Mojolicious::Sessions::ThreeS::Storage> | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | 9 | ▶ | sub get_session{ | ||||||||
|
Subroutine
get_session: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 53 | 9 | my ($self, $session_id) = @_; | |||||||||
| 54 | 9 | my $value = $self->chi->get( $session_id ); | |||||||||
| 55 | 9 | ▶ | unless( $value ){ return; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 7 | return $self->json()->decode( $value ); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | =head2 store_session | ||||||||||
| 60 | |||||||||||
| 61 | See L<Mojolicious::Sessions::ThreeS::Storage> | ||||||||||
| 62 | |||||||||||
| 63 | =cut | ||||||||||
| 64 | |||||||||||
| 65 | 9 | ▶ | sub store_session { | ||||||||
|
Subroutine
store_session: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 66 | 9 | my ( $self, $session_id, $session ) = @_; | |||||||||
| 67 | 9 | my $expires = $session->{expires}; | |||||||||
| 68 | 9 | my $value = $self->json()->encode($session); | |||||||||
| 69 | 9 | ▶ | unless( defined $expires ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 1 | $expires = time() + $self->default_expiration(); | |||||||||
| 71 | } | ||||||||||
| 72 | $self->chi->set( | ||||||||||
| 73 | 9 | $session_id, | |||||||||
| 74 | $value, | ||||||||||
| 75 | { expires_at => $expires, expires_variance => 0.15 } | ||||||||||
| 76 | ); | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | =head2 remove_session_id | ||||||||||
| 80 | |||||||||||
| 81 | See L<Mojolicious::Sessions::ThreeS::Storage> | ||||||||||
| 82 | |||||||||||
| 83 | =cut | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub remove_session_id{ | ||||||||
|
Subroutine
remove_session_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 86 | 0 | my ($self, $session_id) = @_; | |||||||||
| 87 | 0 | $self->chi->remove( $session_id ); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 3 | 1; | |||||||||