Apache-Session-MongoDB

 view release on metacpan or  search on metacpan

lib/Apache/Session/MongoDB.pm  view on Meta::CPAN

    $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



( run in 2.991 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )