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
|
package Apache::Session::MongoDB;
use 5.010;
use strict;
use AutoLoader 'AUTOLOAD';
our $VERSION = '0.23';
our @ISA = qw(Apache::Session);
use Apache::Session;
use Apache::Session::Lock::Null;
use Apache::Session::Store::MongoDB;
use Apache::Session::Generate::MD5;
use Apache::Session::Serialize::MongoDB;
sub populate {
my $self = shift;
$self->{object_store} = Apache::Session::Store::MongoDB->new($self);
$self->{lock_manager} = Apache::Session::Lock::Null->new($self);
$self->{generate} = \&Apache::Session::Generate::MD5::generate;
$self->{validate} = \&Apache::Session::Generate::MD5::validate;
$self->{serialize} = \&Apache::Session::Serialize::MongoDB::serialize;
$self->{unserialize} = \&Apache::Session::Serialize::MongoDB::unserialize;
return $self;
}
our $default;
*default = \$Apache::Session::Store::MongoDB::default;
1;
__END__
sub searchOn {
my ( $class, $args, $selectField, $value, @fields ) = @_;
return $class->_query( $args, { $selectField => $value }, @fields );
}
sub searchOnExpr {
my ( $class, $args, $selectField, $value, @fields ) = @_;
$value =~ s/([\/\\\^\$\.\?\+\[\]\{\}\(\)])/\\$1/g;
$value =~ s/\*/\.\*/g;
return $class->_query( $args, { $selectField => qr/$value/i } );
}
sub deleteIfLowerThan {
my ( $class, $args, $rule ) = @_;
my $query;
if ( $rule->{or} ) {
$query = {
'$or' => [
map {
{ $_ => { '$lt' => $rule->{or}->{$_} } }
}
keys %{ $rule->{or} }
]
};
}
elsif ( $rule->{and} ) {
$query = {
'$and' => [
map {
{ $_ => { '$lt' => $rule->{or}->{$_} } }
}
keys %{ $rule->{or} }
]
};
}
if ( $rule->{not} ) {
$query = {
'$and' => [
$query,
map {
{ $_ => { '$ne' => $rule->{not}->{$_} } }
}
keys %{ $rule->{not} }
]
};
}
return 0 unless ($query);
my $col = $class->_col($args);
my $result = $col->delete_many($query);
if ( $result->acknowledged ) {
if (wantarray) {
return 1, $result->deleted_count;
}
else {
return 1;
}
}
else {
return 1;
}
}
sub _query {
my ( $class, $args, $filter, @fields ) = @_;
my $col = $class->_col($args);
my $cursor = $col->find($filter);
if (@fields) {
$cursor =
$cursor->fields( { map { $_ => 1 } @fields, "_session_id" => 1 } );
}
my %res;
while ( my $r = $cursor->next ) {
my $id = $r->{_session_id};
delete $r->{_id};
$res{$id} = $r;
}
return \%res;
}
sub get_key_from_all_sessions {
my ( $class, $args, $data ) = @_;
my $col = $class->_col($args);
my $cursor = $col->find( {} );
my %res;
while ( my $r = $cursor->next ) {
my $id = $r->{_session_id};
delete $r->{_id};
if ( ref($data) eq 'CODE' ) {
$res{$id} = $data->( $r, $id );
}
elsif ($data) {
$data = [$data] unless ( ref $data );
$res{$id}->{$_} = $r->{$_} foreach (@$data);
}
else {
$res{$id} = $r;
}
}
return \%res;
}
sub _col {
my ( $self, $args ) = @_;
my $conn_args;
foreach my $w (
qw(host auth_mechanism auth_mechanism_properties bson_codec
connect_timeout_ms db_name heartbeat_frequency_ms j local_threshold_ms
max_time_ms password port read_pref_mode read_pref_tag_sets
replica_set_name server_selection_timeout_ms server_selection_try_once
socket_check_interval_ms socket_timeout_ms ssl username w wtimeout
read_concern_level)
)
{
$conn_args->{$w} = $args->{$w} || $default->{$w};
delete $conn_args->{$w} unless ( defined $conn_args->{$w} );
}
my $s = MongoDB->connect( $args->{host} || $default->{host}, $conn_args )
or die('Unable to connect to MongoDB server');
return $s->get_database( $args->{db_name} || $default->{db_name} )
->get_collection( $args->{collection} || $default->{collection} );
}
1;
=head1 NAME
Apache::Session::MongoDB - An implementation of Apache::Session
=head1 SYNOPSIS
use Apache::Session::MongoDB;
# Using localhost server
tie %hash, 'Apache::Session::MongoDB', $id, {};
# Example with default values
tie %hash, 'Apache::Session::MongoDB', $id, {
host => 'locahost:27017',
db_name => 'sessions',
collection => 'sessions',
};
=head1 DESCRIPTION
This module is an implementation of Apache::Session. It uses the MongoDB
backing store and no locking.
=head1 PARAMETERS
You can set the followong parameters host, db_name, collection, auth_mechanism,
auth_mechanism_properties, connect_timeout_ms, ssl, username and password.
See L<MongoDB> for more
=head1 SEE ALSO
L<MongoDB>, L<Apache::Session>
=head1 AUTHOR
Xavier Guimard, E<lt>x.guimard@free.frE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2015-2016 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>
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.0 or,
at your option, any later version of Perl 5 you may have available.
=cut
|