| 1 | package Prancer::Session::Store::Database; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use version; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | our $VERSION = '1.01'; | |
| 8 | |||
| 9 | 1 | 1; | |
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Prancer::Session::Store::Database | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | This module implements a session handler that stores sessions in a database. It | ||
| 18 | creates its own database connection, separate from any existing database | ||
| 19 | connection, to avoid any issues with transactions. It wraps all changes to the | ||
| 20 | database in transactions to ensure consistency. | ||
| 21 | |||
| 22 | This configuration expects a database table that looks like this: | ||
| 23 | |||
| 24 | CREATE TABLE session ( | ||
| 25 | id CHAR(72) NOT NULL, | ||
| 26 | application VARCHAR DEFALUT '' NOT NULL, | ||
| 27 | timeout integer DEFAULT date_part('epoch'::text, now()) NOT NULL, | ||
| 28 | data TEXT | ||
| 29 | ); | ||
| 30 | |||
| 31 | CREATE UNIQUE INDEX session_uq ON sessions (id, application); | ||
| 32 | CREATE INDEX session_timeout_ix ON sessions (timeout); | ||
| 33 | |||
| 34 | Additional columns may be added as desired but they will not be used by this | ||
| 35 | session handler. | ||
| 36 | |||
| 37 | To use this session handler, add this to your configuration file: | ||
| 38 | |||
| 39 | session: | ||
| 40 | store: | ||
| 41 | driver: Prancer::Session::Store::Database::Driver::DriverName | ||
| 42 | options: | ||
| 43 | table: sessions | ||
| 44 | database: test | ||
| 45 | username: test | ||
| 46 | password: test | ||
| 47 | hostname: localhost | ||
| 48 | port: 5432 | ||
| 49 | charset: utf8 | ||
| 50 | connection_check_threshold: 10 | ||
| 51 | dsn_extra: | ||
| 52 | RaiseError: 0 | ||
| 53 | PrintError: 1 | ||
| 54 | on_connect: | ||
| 55 | - SET search_path=public | ||
| 56 | expiration_timeout: 3600 | ||
| 57 | autopurge: 0 | ||
| 58 | autopurge_probability: 0.1 | ||
| 59 | application: foobar | ||
| 60 | |||
| 61 | =head1 OPTIONS | ||
| 62 | |||
| 63 | =over | ||
| 64 | |||
| 65 | =item table | ||
| 66 | |||
| 67 | The name of the table in your database to use to store sessions. This name may | ||
| 68 | include a schema name, like C<public.sessions>. Otherwise the default schema of | ||
| 69 | the database user will be used. If this option is not provided then the default | ||
| 70 | table name is C<sessions>. | ||
| 71 | |||
| 72 | =item database | ||
| 73 | |||
| 74 | B<REQUIRED> The name of the database to connect to. If using SQLite, this | ||
| 75 | should be the path to the database file. | ||
| 76 | |||
| 77 | =item username | ||
| 78 | |||
| 79 | The username to use when connecting. If this option is not set the default is | ||
| 80 | the user running the application server. If using SQLite then this will be | ||
| 81 | ignored. | ||
| 82 | |||
| 83 | =item password | ||
| 84 | |||
| 85 | The password to use when connecting. If this option is not set the default is | ||
| 86 | to connect with no password. If using SQLite then this will be ignored. | ||
| 87 | |||
| 88 | =item hostname | ||
| 89 | |||
| 90 | The host name of the database server. If this option is not set the default is | ||
| 91 | to connect to localhost. If using SQLite then this will be ignored. | ||
| 92 | |||
| 93 | =item port | ||
| 94 | |||
| 95 | The port number on which the database server is listening. If this option is | ||
| 96 | not set the default is to connect on the database's default port. If using | ||
| 97 | SQLite then this will be ignored. | ||
| 98 | |||
| 99 | =item charset | ||
| 100 | |||
| 101 | The character set to connect to the database with. If this is set to "utf8" | ||
| 102 | then the database connection will attempt to make UTF8 data Just Work if | ||
| 103 | available. | ||
| 104 | |||
| 105 | =item connection_check_threshold | ||
| 106 | |||
| 107 | This sets the number of seconds that must elapse between calls to get a | ||
| 108 | database handle before performing a check to ensure that a live database | ||
| 109 | connection still exists. If the check for a live database connection fails then | ||
| 110 | the session handler will attempt to reconnect. This handles cases where the | ||
| 111 | database handle hasn't been used in a while and the underlying connection has | ||
| 112 | gone away. If this is not set it will default to 30 seconds. | ||
| 113 | |||
| 114 | =item dsn_extra | ||
| 115 | |||
| 116 | If you have any further connection parameters that need to be appended to the | ||
| 117 | dsn then you can put them in the configuration as a hash. This hash will be | ||
| 118 | merged into the default parameters and overwrite any that are duplicated. The | ||
| 119 | dsn parameters set by default are C<AutoCommit> to 0, C<RaiseError> to 1, and | ||
| 120 | C<PrintError> to 0. | ||
| 121 | |||
| 122 | =item on_connect | ||
| 123 | |||
| 124 | This can be an array of commands execute on a successful connection. These will | ||
| 125 | be executed on every connection so if the connection goes away but is re- | ||
| 126 | established then these commands will be run again. | ||
| 127 | |||
| 128 | =item timeout | ||
| 129 | |||
| 130 | Tthis is the number of seconds a session should last in the database before it | ||
| 131 | will be automatically purged. The default is to purge sessions after 1800 | ||
| 132 | seconds (30 minutes). | ||
| 133 | |||
| 134 | =item autopurge | ||
| 135 | |||
| 136 | This flag controls whether sessions will be automatically purged by Prancer. | ||
| 137 | If set to 1, the default, then on 10% of requests to your application, Prancer | ||
| 138 | will delete from the database any session that has timed out. If set to 0 then | ||
| 139 | sessions will never be removed from the database. Note that this doesn't | ||
| 140 | control whether sessions time out, only whether they get removed from the | ||
| 141 | database. | ||
| 142 | |||
| 143 | =item autopurge_probability | ||
| 144 | |||
| 145 | This is the probability that autopurge will run on any given request. By | ||
| 146 | default, this value is 0.1, or 10%, meaning that 1 in every 10 requests will | ||
| 147 | attempt to purge expired sessions. This can be set to "1" to purge on every | ||
| 148 | session action or to something extremely small like 0.001 to purge very, very | ||
| 149 | infrequently. Or you can export purging duties to another program entirely. | ||
| 150 | |||
| 151 | =item application | ||
| 152 | |||
| 153 | If multiple applications will be using the same session table then this option | ||
| 154 | may be used to distinguish between them. This key will be used in the | ||
| 155 | C<application> column of the sessions table. | ||
| 156 | |||
| 157 | =back | ||
| 158 | |||
| 159 | =head1 COPYRIGHT | ||
| 160 | |||
| 161 | Copyright 2014 Paul Lockaby. All rights reserved. | ||
| 162 | |||
| 163 | This library is free software; you can redistribute it and/or modify it under | ||
| 164 | the same terms as Perl itself. | ||
| 165 | |||
| 166 | =head1 SEE ALSO | ||
| 167 | |||
| 168 | =over | ||
| 169 | |||
| 170 | =item | ||
| 171 | |||
| 172 | L<Prancer> | ||
| 173 | |||
| 174 | =item | ||
| 175 | |||
| 176 | L<Prancer::Session> | ||
| 177 | |||
| 178 | =back | ||
| 179 | |||
| 180 | =cut | ||