NAME

Web::Session::MySQLJSON - Add index and search methods to Apache::Session::MySQL

SYNOPSIS

Create table. Example for Lemonldap::NG with optional generated columns and indexes:

CREATE TABLE sessions (
    id varchar(64) not null primary key,
    a_session json,
    as_wt varchar(255) COLLATE utf8mb4_bin
        AS (a_session->>'$._whatToTrace') VIRTUAL,
    as_sk varchar(32) COLLATE utf8mb4_bin
        AS (a_session->>'$._session_kind') VIRTUAL,
    as_ut bigint unsigned
        AS (cast(a_session->>'$._utime' as unsigned)) VIRTUAL,
    as_ls bigint unsigned
        AS (cast(a_session->>'$._lastSeen' as unsigned)) VIRTUAL,
    as_rt bigint unsigned
        AS (cast(a_session->>'$._oidcRtUpdate' as unsigned)) VIRTUAL,
    as_ip varchar(64) COLLATE utf8mb4_bin
        AS (a_session->>'$.ipAddr') VIRTUAL,
    KEY as_wt (as_wt),
    KEY as_sk (as_sk),
    KEY as_ut (as_ut),
    KEY as_ls (as_ls),
    KEY as_rt (as_rt),
    KEY as_ip (as_ip)
) ENGINE=InnoDB;

MySQL uses the index of a generated column only when the query contains the same expression, so keep these expressions exactly as written: they are the ones used by this module (->> returns unquoted values whereas -> keeps JSON quotes). MySQL doesn't use them for LIKE queries, so searchOnExpr() always scans the table.

Searches are exact (case and accent sensitive). Generated text columns must use the utf8mb4_bin collation: otherwise, searches on indexed fields follow the table collation (case and accent insensitive with utf8mb4_0900_ai_ci, the default of MySQL 8) whereas searches on other fields don't.

Exception: utf8mb4_bin is a PAD SPACE collation, so = ignores trailing spaces: an indexed search for 'dwho ' finds the sessions of dwho, whereas a search on a non indexed field (done by Perl) doesn't. To avoid it, use a NO PAD collation: utf8mb4_0900_bin on MySQL >= 8.0.17 (utf8mb4_nopad_bin on MariaDB).

as_ls is needed when Lemonldap::NG "timeoutActivity" is used: sessions purge then deletes sessions whose _utime or _lastSeen is too old, and an OR with a non indexed side forces a full table scan (with both keys, MySQL uses an index_merge).

as_rt is useful when OpenID Connect relying parties have a refresh token activity timeout: sessions purge then calls searchLt() on _oidcRtUpdate.

With strict SQL mode (default since MySQL 5.7), a value that doesn't fit its indexed generated column makes the session storage fail: a string longer than the column, or a non integer _utime, _lastSeen or _oidcRtUpdate value (cast('1.5' as unsigned) is an error in this case). Size varchar columns generously; Lemonldap::NG always writes integer _utime, _lastSeen and _oidcRtUpdate. These values must be non negative integers: a JSON null (for example _lastSeen => undef) or a negative number is refused too, since ->> returns the string 'null' (not SQL NULL) and the session can't be written anymore. An absent key is fine (the column is NULL).

Generated columns documented by previous versions (using ->) were never used. To fix an existing table, drop those you created:

ALTER TABLE sessions DROP COLUMN as_wt, DROP COLUMN as_sk,
    DROP COLUMN as_ut, DROP COLUMN as_ip;

then create the new ones:

ALTER TABLE sessions
    ADD COLUMN as_wt varchar(255) COLLATE utf8mb4_bin
        AS (a_session->>'$._whatToTrace') VIRTUAL,
    ADD COLUMN as_sk varchar(32) COLLATE utf8mb4_bin
        AS (a_session->>'$._session_kind') VIRTUAL,
    ADD COLUMN as_ut bigint unsigned
        AS (cast(a_session->>'$._utime' as unsigned)) VIRTUAL,
    ADD COLUMN as_ls bigint unsigned
        AS (cast(a_session->>'$._lastSeen' as unsigned)) VIRTUAL,
    ADD COLUMN as_rt bigint unsigned
        AS (cast(a_session->>'$._oidcRtUpdate' as unsigned)) VIRTUAL,
    ADD COLUMN as_ip varchar(64) COLLATE utf8mb4_bin
        AS (a_session->>'$.ipAddr') VIRTUAL,
    ADD KEY as_wt (as_wt), ADD KEY as_sk (as_sk),
    ADD KEY as_ut (as_ut), ADD KEY as_ls (as_ls), ADD KEY as_rt (as_rt),
    ADD KEY as_ip (as_ip);

Use it with Perl:

use Web::Session::MySQLJSON;

my $args = {
     DataSource => 'dbi:mysql:sessions',
     UserName   => $db_user,
     Password   => $db_pass,
};

Don't set Index: any field can be searched from a_session and the store would try to write indexed fields into columns of the same name, which don't exist in a JSON table. populate() warns when Index is set.

Field names and searched values are strings: a non flagged string containing bytes 0x80 or more is first tried as UTF-8 and read as Latin-1 if it is not valid UTF-8, so both encodings find non-ASCII values.

Use it like the other backends of Web::Session. Web::Session::MySQL is an alias of this module.

DESCRIPTION

Web::Session provides some class methods to manipulate all sessions and add the capability to index some fields to make research faster.

Web::Session::MySQLJSON implements it for MySQL databases using "json" type to be able to browse sessions.

searchLt() and searchGt()

# Sessions whose _utime is lower than $time
my $hash = Web::Session::MySQLJSON->searchLt( $args,
    '_utime', $time, 'uid' );

searchLt() and searchGt() take the same arguments and return the same data as searchOn(): sessions whose field is lower (or greater) than the given value, which is excluded. The value must be a number (12, -12 or 12.5): otherwise nothing is returned and an error is printed on STDERR. Spaces around the value are ignored.

Fields are compared in SQL as deleteIfLowerThan() does (cast(a_session->>'$.field' as unsigned)), so the index of a generated column declared as as_ut or as_rt above is used. Values are compared as unsigned integers: a non-numeric value is 0.

Sessions without the field are never returned. This differs from the Perl fallback of Lemonldap::NG, where a missing field is compared as 0: searchLt() would then return nearly all sessions. Lemonldap::NG doesn't need them: its sessions purge ignores sessions without _oidcRtUpdate.

Because of the unsigned cast, two cases differ from a numeric comparison: a field stored as JSON null is 0 (->> returns the string null), so searchLt() returns it, and a negative value becomes a huge number (-5 is found by searchGt( $args, $field, 50 )). Lemonldap::NG timestamps are positive integers.

This module isn't usable with MariaDB: use Web::Session::MariaDBJSON instead.

Persistent connection

The database connection is kept open between sessions. Set noreuse => 1 to open and close one for each session, see "PERSISTENT DATABASE CONNECTIONS" in Web::Session.

SEE ALSO

Apache::Session, Web::Session::MySQL, Web::Session::MariaDBJSON, http://lemonldap-ng.org

COPYRIGHT AND LICENSE

2009-2026 by Xavier Guimard
2013-2025 by Clément Oudot
2019-2025 by Maxime Besson
2013-2025 by Worteks
2023-2026 by Linagora

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.