| 1 | package Dancer::Session::CHI; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use CHI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Dancer ":syntax"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Dancer::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Dancer::ModuleLoader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Dancer::Exception qw(raise); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use File::Spec::Functions qw(rel2abs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use base "Dancer::Session::Abstract"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | our $VERSION = 'v0.1.6'; # VERSION | |||||||||||||||||||||||||||
| 17 | # ABSTRACT: CHI-based session engine for Dancer | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Class methods: | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | my $chi; | |||||||||||||||||||||||||||
| 22 | 5 | ▶ | sub _chi { | ||||||||||||||||||||||||||
|
Subroutine
_chi: called CC=8 SCAR=21.1
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | return $chi if blessed($chi); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | my $options = setting("session_CHI"); | |||||||||||||||||||||||||||
| 27 | 5 | ▶ | if ( ref($options) ne "HASH" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 1 | raise core_session => "CHI session options not found"; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Don't let CHI determine the absolute path: | ||||||||||||||||||||||||||||
| 32 | 4 | ▶ | if ( exists $options->{root_dir} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | $options->{root_dir} = rel2abs($options->{root_dir}); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | my $use_plugin = delete $options->{use_plugin}; | |||||||||||||||||||||||||||
| 37 | 4 | my $is_loaded = exists setting("plugins")->{"Cache::CHI"}; | |||||||||||||||||||||||||||
| 38 | 4 | ▶ | if ( $use_plugin && !$is_loaded ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$use_plugin and not $is_loaded
MC/DC100%2/2
$use_plugin and not $is_loaded
$use_plugin
not $is_loaded
Truth table100%3/3
A and not(B)
A= $use_pluginB= not $is_loaded
| |||||||||||||||||||||||||||||
| 39 | 1 | raise core_session => "CHI plugin requested but not loaded"; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | $chi = $use_plugin | ||||||||||||||||||||||||||||
| 43 | 3 | ▶ | ? do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 2 | my $plugin = "Dancer::Plugin::Cache::CHI"; | |||||||||||||||||||||||||||
| 45 | 2 | ▶ | unless ( Dancer::ModuleLoader->load($plugin) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | raise core_session => "$plugin is needed and is not installed"; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | Dancer::Plugin::Cache::CHI::cache() | ||||||||||||||||||||||||||||
| 49 | 2 | } | |||||||||||||||||||||||||||
| 50 | : CHI->new(%$options); | ||||||||||||||||||||||||||||
| 51 | 3 | return $chi; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | ▶ | sub create { | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 4 | my $self = __PACKAGE__->new->flush; | |||||||||||||||||||||||||||
| 56 | 2 | Dancer::Logger->debug("Session (id: " . $self->id . " created."); | |||||||||||||||||||||||||||
| 57 | 2 | return $self; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub retrieve { | ||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 1 | my (undef, $session_id) = @_; | |||||||||||||||||||||||||||
| 62 | 1 | return _chi->get("session_$session_id"); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Object methods: | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 69 | 4 | my $session_id = $self->id; | |||||||||||||||||||||||||||
| 70 | 4 | _chi->set( "session_$session_id" => $self ); | |||||||||||||||||||||||||||
| 71 | 2 | return $self; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub purge { | ||||||||||||||||||||||||||
|
Subroutine
purge: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | _chi->purge; | |||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | #sub reset :method { goto &purge } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub destroy { | ||||||||||||||||||||||||||
|
Subroutine
destroy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 83 | 0 | my $session_id = $self->id; | |||||||||||||||||||||||||||
| 84 | 0 | _chi->remove("session_$session_id"); | |||||||||||||||||||||||||||
| 85 | 0 | cookies->{setting("session_name")}->expires(0); | |||||||||||||||||||||||||||
| 86 | 0 | Dancer::Logger->debug("Session (id: $session_id) destroyed."); | |||||||||||||||||||||||||||
| 87 | 0 | return $self; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 4 | 1; | |||||||||||||||||||||||||||
| 91 | =encoding utf8 | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 NAME | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Dancer::Session::CHI - CHI-based session engine for Dancer | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | In a L<Dancer> application: | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | set session => "CHI"; | ||||||||||||||||||||||||||||
| 102 | set session_expires => "1 hour"; | ||||||||||||||||||||||||||||
| 103 | set session_CHI => { use_plugin => 1 }; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | set plugins => { | ||||||||||||||||||||||||||||
| 106 | "Cache::CHI" => { | ||||||||||||||||||||||||||||
| 107 | driver => 'Memory', | ||||||||||||||||||||||||||||
| 108 | global => 1 | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | In a F<config.yml>: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | session: CHI | ||||||||||||||||||||||||||||
| 115 | session_expires: 1 hour | ||||||||||||||||||||||||||||
| 116 | session_CHI: | ||||||||||||||||||||||||||||
| 117 | use_plugin: 1 | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | plugins: | ||||||||||||||||||||||||||||
| 120 | Cache::CHI: | ||||||||||||||||||||||||||||
| 121 | driver: Memory | ||||||||||||||||||||||||||||
| 122 | global: 1 | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | This module leverages L<CHI> to provide session management for L<Dancer> | ||||||||||||||||||||||||||||
| 127 | applications. Just as L<Dancer::Session::KiokuDB> opens up L<KiokuDB>'s | ||||||||||||||||||||||||||||
| 128 | full range of C<KiokuDB::Backend>::* modules to be used in Dancer session | ||||||||||||||||||||||||||||
| 129 | management, L<Dancer::Session::CHI> makes available the complete | ||||||||||||||||||||||||||||
| 130 | C<CHI::Driver>::* collection. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Under its C<session_CHI> key, Dancer::Session::CHI accepts a C<use_plugin> | ||||||||||||||||||||||||||||
| 135 | option that defaults to C<0>. If set to C<1>, L<Dancer::Plugin::Cache::CHI> | ||||||||||||||||||||||||||||
| 136 | will be used directly for session management, with no changes made to the | ||||||||||||||||||||||||||||
| 137 | plugin's configuration. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | If C<use_plugin> is left false, all other options are passed through to | ||||||||||||||||||||||||||||
| 140 | construct a new L<CHI> object, even if L<Dancer::Plugin::Cache::CHI> is also in | ||||||||||||||||||||||||||||
| 141 | use. This new object needn't use the same L<CHI::Driver> as the plugin. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 METHODS | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =for Pod::Coverage BUILD | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 CLASS | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =over | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item C<create()> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Creates a new session object and returns it. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =item C<retrieve($id)> | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Returns the session object containing an ID of $id. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =back | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 OBJECT | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =over | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =item C<flush()> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Writes all session data to the CHI storage backend. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item C<destroy()> | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Ends a Dancer session and wipes the session's data from the CHI storage backend. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =item C<purge()> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Direct access to CHI's C<purge()> method, clearing the data of all expired | ||||||||||||||||||||||||||||
| 176 | sessions from the CHI storage backend. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =back | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 CAVEATS | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =over | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item * | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Some L<CHI::Driver> parameters are sufficiently complex to not be placeable in a F<config.yml>. Session and/or | ||||||||||||||||||||||||||||
| 187 | plugin configuration may instead be needed to be done in application code. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =item * | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | When using L<CHI::Driver::DBI>, thread/fork safety can be ensured by passing it | ||||||||||||||||||||||||||||
| 192 | a L<DBIx::Connector> object or database handle returned by | ||||||||||||||||||||||||||||
| 193 | L<Dancer::Plugin::Database>'s C<database()> subroutine. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =back | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 BUGS | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | This is an initial I<TRIAL> release, so bugs may be lurking. Please report any issues to this module's | ||||||||||||||||||||||||||||
| 200 | L<GitHub issues page|https://github.com/rsimoes/Dancer-Session-CHI/issues>. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Richard Simões <rsimoes at CPAN dot org> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Copyright © 2013 Richard Simões. This module is released under the terms of the | ||||||||||||||||||||||||||||
| 209 | B<MIT License> and may be modified and/or redistributed under the same or any | ||||||||||||||||||||||||||||
| 210 | compatible license. | ||||||||||||||||||||||||||||