NAME
Web::Session::Redis - Add index and search methods to Apache::Session::Redis
SYNOPSIS
use Web::Session::Redis;
my $args = {
server => '127.0.0.1:6379',
# Select database (optional)
#database => 0,
# Use a persistent connection to the Redis server
# (value is the connection cache key)
# You'll probably also want to set
# read_timeout, write_timeout, reconnect and every
reuse => "myserver",
# Choose your browseable fields
Index => 'uid mail',
# Optional: set a Redis TTL on session keys (in seconds)
# TTL => 86400,
};
# Use it like Apache::Session
my %session;
tie %session, 'Web::Session::Redis', $id, $args;
$session{uid} = 'me';
$session{mail} = 'me@me.com';
$session{unindexedField} = 'zz';
untie %session;
# Web::Session add some global class methods
#
# 1) search on a field (indexed or not)
my $hash = Web::Session::Redis->searchOn( $args, 'uid', 'me' );
foreach my $id (keys %$hash) {
print $id . ":" . $hash->{$id}->{mail} . "\n";
}
# 2) Parse all sessions
# a. get all sessions
my $hash = Web::Session::Redis->get_key_from_all_sessions($args);
# b. get some fields from all sessions
my $hash = Web::Session::Redis->get_key_from_all_sessions($args, 'uid', 'mail')
# c. execute something with datas from each session :
# Example : get uid and mail if mail domain is
my $hash = Web::Session::Redis->get_key_from_all_sessions(
$args,
sub {
my ( $session, $id ) = @_;
if ( $session->{mail} =~ /mydomain.com$/ ) {
return { $session->{uid}, $session->{mail} };
}
}
);
foreach my $id (keys %$hash) {
print $id . ":" . $hash->{$id}->{uid} . "=>" . $hash->{$id}->{mail} . "\n";
}
DESCRIPTION
Web::Session provides some class methods to manipulate all sessions and add the capability to index some fields to make research faster.
This module use either Redis::Fast or Redis.
searchLt() AND searchGt()
# Sessions where _oidcRtUpdate < $time (or > $time)
my $hash = Web::Session::Redis->searchLt(
$args, '_oidcRtUpdate', $time );
$hash = Web::Session::Redis->searchGt(
$args, '_oidcRtUpdate', $time, 'uid', 'client_id' );
They return sessions whose field is strictly lower (or greater) than the given value, like searchOn(): { id => session }, or only the requested fields. The comparison is numeric.
Leading and trailing spaces of the value are removed, then it must match /^-?[0-9]+(?:\.[0-9]+)?\z/. Otherwise, an error is printed on STDERR and an empty hash is returned.
Sessions where the field is missing, empty or not a number (same check as the value, so Inf or 1e3 are skipped) are never returned. This differs from the SQL backends, which compare a non-numeric value as 0 (and cast its numeric prefix: 250x is 250), so searchLt($args, 'f', 10) returns a session where f is abc with them, but not with Redis. Numbers must be written in plain decimal form to be found. Likewise, the Perl fallback of Lemonldap::NG, used with backends that don't provide these methods, compares a missing field as 0.
If the field isn't listed in Index, all sessions are read and decoded. If it is, Redis still walks the whole keyspace (SCAN ... MATCH field_*), but server-side: only the index sets of this field (field_value keys) are returned, and only the sessions of sets holding a matching value are read. Orphan entries of these sets are removed; keys that don't match keysRe are ignored.
For the Lemonldap::NG purge of OpenID Connect refresh tokens, add _oidcRtUpdate to Index. Sessions stored before a field is added to Index are not in its index until they are saved again, and refresh tokens are saved only when used: inactive ones won't be found by searchLt() and will only be purged when _utime expires. To index them at once, re-save them:
my $rt = Web::Session::Redis->get_key_from_all_sessions(
$args, sub { $_[0]->{_oidcRtUpdate} } );
foreach my $id ( keys %$rt ) {
tie my %s, 'Web::Session::Redis', $id, $args;
$s{_oidcRtUpdate} = $s{_oidcRtUpdate};
untie %s;
}
This also refreshes their TTL, if any.
CHARACTERS ABOVE U+00FF
Redis clients refuse characters above U+00FF. Sessions are stored as JSON where such characters (and only them) are written as \uXXXX escapes, so they are supported: the stored value is the same as before for a session holding only Latin-1 characters. Index set names (field_value) are sent as Latin-1 bytes when they can be, so existing sets keep working, and as UTF-8 bytes otherwise. The name of a value above U+00FF may then be the same as the Latin-1 name of another value (uid_\x{3a9} and uid_\x{ce}\x{a9}).
Index sets are updated after the session is written, without a transaction. If the same session is updated concurrently by two processes, a stale member can remain in the index set of a previous value. searchOn only removes members whose session no longer exists and doesn't check the indexed value again, so such a session may be returned for a value it no longer has.
SEE ALSO
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.