1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
package Apache::Session::Browseable::Redis;
use strict;
use Apache::Session;
use Apache::Session::Browseable::Store::Redis;
use Apache::Session::Generate::SHA256;
use Apache::Session::Lock::Null;
use Apache::Session::Serialize::JSON;
use Apache::Session::Browseable::_common;
our $VERSION = '1.3.16';
our @ISA = qw(Apache::Session);
our $redis = $Apache::Session::Browseable::Store::Redis::redis;
sub populate {
my $self = shift;
$self->{object_store} = new Apache::Session::Browseable::Store::Redis $self;
$self->{lock_manager} = new Apache::Session::Lock::Null $self;
$self->{generate} = \&Apache::Session::Generate::SHA256::generate;
$self->{validate} = \&Apache::Session::Generate::SHA256::validate;
$self->{serialize} = \&Apache::Session::Serialize::JSON::serialize;
$self->{unserialize} = \&Apache::Session::Serialize::JSON::unserialize;
return $self;
}
sub unserialize {
my $session = shift;
my $tmp = { serialized => $session };
Apache::Session::Serialize::JSON::unserialize($tmp);
return $tmp->{data};
}
sub searchOn {
my ( $class, $args, $selectField, $value, @fields ) = @_;
my %res = ();
if ( $class->isIndexed( $args, $selectField ) ) {
my $redisObj = $class->_getRedis($args);
my @keys = $redisObj->smembers("${selectField}_$value");
foreach my $k (@keys) {
next unless ($k);
my $tmp = $redisObj->get($k);
next unless ($tmp);
eval {
$tmp = unserialize($tmp);
if (@fields) {
$res{$k}->{$_} = $tmp->{$_} foreach (@fields);
}
else {
$res{$k} = $tmp;
}
};
if ($@) {
print STDERR "Error in session $k: $@\n";
delete $res{$k};
}
}
}
else {
$class->get_key_from_all_sessions(
$args,
sub {
my $entry = shift;
my $id = shift;
return undef
unless ( defined $entry->{$selectField}
and $entry->{$selectField} eq $value );
if (@fields) {
$res{$id}->{$_} = $entry->{$_} foreach (@fields);
}
else {
$res{$id} = $entry;
}
undef;
}
);
}
return \%res;
}
sub searchOnExpr {
my ( $class, $args, $selectField, $value, @fields ) = @_;
my %res;
if ( $class->isIndexed( $args, $selectField ) ) {
my $redisObj = $class->_getRedis($args);
my $cursor = 0;
do {
my ( $new_cursor, $sets ) =
$redisObj->scan( $cursor, MATCH => "${selectField}_$value" );
foreach my $set (@$sets) {
next unless $redisObj->type($set) eq 'set';
my @keys = $redisObj->smembers($set);
foreach my $k (@keys) {
my $v = $redisObj->get($k);
next unless $v;
my $tmp = unserialize($v);
if ($tmp) {
$res{$k} = $class->extractFields( $tmp, @fields );
}
}
}
$cursor = $new_cursor;
} while ( $cursor != 0 );
}
else {
$value = quotemeta($value);
$value =~ s/\\\*/\.\*/g;
$value = qr/^$value$/;
$class->get_key_from_all_sessions(
$args,
sub {
my ( $entry, $id ) = @_;
return undef unless ( $entry->{$selectField} =~ $value );
$res{$id} = $class->extractFields( $entry, @fields );
undef;
}
);
}
return \%res;
}
sub deleteIfLowerThan {
my ( $class, $args, $rule ) = @_;
my $deleted = 0;
my $redisObj = $class->_getRedis($args);
$class->get_key_from_all_sessions(
$args,
sub {
my ( $v, $k ) = @_;
if ( $rule->{not} ) {
foreach ( keys %{ $rule->{not} } ) {
if (defined( $v->{$_} ) and $v->{$_} eq $rule->{not}->{$_}) {
return ();
}
}
}
if ( $rule->{or} ) {
foreach ( keys %{ $rule->{or} } ) {
if ( defined( $v->{$_} ) and $v->{$_} < $rule->{or}->{$_} )
{
$redisObj->del($k);
$deleted++;
return ();
}
}
}
elsif ( $rule->{and} ) {
my $res = 1;
foreach ( keys %{ $rule->{and} } ) {
$res = 0
unless defined( $v->{$_} )
and $v->{$_} < $rule->{not}->{$_};
}
if ($res) {
$redisObj->del($k);
$deleted++;
}
}
return ();
},
);
return ( 1, $deleted );
}
sub extractFields {
my ( $class, $entry, @fields ) = @_;
my $res;
if (@fields) {
$res->{$_} = $entry->{$_} foreach (@fields);
}
else {
$res = $entry;
}
return $res;
}
sub isIndexed {
my ( $class, $args, $field ) = @_;
my $indexes =
ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ];
return grep { $_ eq $field } @$indexes;
}
sub isLlngKey {
my ( $class, $args, $name ) = @_;
my $expr = $args->{keysRe} || '^[0-9a-f]{32,}$';
return ( $name =~ /$expr/o );
}
sub get_key_from_all_sessions {
my ( $class, $args, $data ) = @_;
my %res;
my $redisObj = $class->_getRedis($args);
my $cursor = 0;
do {
my ( $new_cursor, $keys ) = $redisObj->scan($cursor);
foreach my $k (@$keys) {
# Keep only our keys
next unless $class->isLlngKey( $args, $k );
# Don't scan sets,...
next unless $redisObj->type($k) eq 'string';
eval {
my $v = $redisObj->get($k);
next unless $v;
my $tmp = unserialize($v);
if ( ref($data) eq 'CODE' ) {
$tmp = &$data( $tmp, $k );
$res{$k} = $tmp if ( defined($tmp) );
}
elsif ($data) {
$data = [$data] unless ( ref($data) );
$res{$k}->{$_} = $tmp->{$_} foreach (@$data);
}
else {
$res{$k} = $tmp;
}
};
if ($@) {
print STDERR "Error in session $k: $@\n";
# Don't delete, it may own to another app
#delete $res{$k};
}
}
$cursor = $new_cursor;
} while ( $cursor != 0 );
return \%res;
}
sub _getRedis {
my $class = shift;
my $args = shift;
# Manage undef encoding
$args->{encoding} = undef
if ( $args->{encoding}
and $args->{encoding} eq "undef" );
# If sentinels is not given as an array ref, try to parse
# a comma delimited list instead
if ( $args->{sentinels}
and ref $args->{sentinels} ne 'ARRAY' )
{
$args->{sentinels} =
[ split /[,\s]+/, $args->{sentinels} ];
}
my $redisObj = $redis->new( %{$args} );
# Manage database
$redisObj->select( $args->{database} )
if defined $args->{database};
return $redisObj;
}
1;
__END__
=head1 NAME
Apache::Session::Browseable::Redis - Add index and search methods to
Apache::Session::Redis
=head1 SYNOPSIS
use Apache::Session::Browseable::Redis;
my $args = {
server => '127.0.0.1:6379',
# Select database (optional)
#database => 0,
# Choose your browseable fields
Index => 'uid mail',
};
# Use it like Apache::Session
my %session;
tie %session, 'Apache::Session::Browseable::Redis', $id, $args;
$session{uid} = 'me';
$session{mail} = 'me@me.com';
$session{unindexedField} = 'zz';
untie %session;
# Apache::Session::Browseable add some global class methods
#
# 1) search on a field (indexed or not)
my $hash = Apache::Session::Browseable::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 = Apache::Session::Browseable::Redis->get_key_from_all_sessions($args);
# b. get some fields from all sessions
my $hash = Apache::Session::Browseable::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 = Apache::Session::Browseable::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";
}
=head1 DESCRIPTION
Apache::Session::browseable provides some class methods to manipulate all
sessions and add the capability to index some fields to make research faster.
This module use either L<Redis::Fast> or L<Redis>.
=head1 SEE ALSO
L<Apache::Session>
=head1 COPYRIGHT AND LICENSE
=over
=item 2009-2025 by Xavier Guimard
=item 2013-2025 by Clément Oudot
=item 2019-2025 by Maxime Besson
=item 2013-2025 by Worteks
=item 2023-2025-2025 by Linagora
=back
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.
=cut
|