MarkAHershberger has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/348099 )

Change subject: Cache LDAP lookups better.
......................................................................

Cache LDAP lookups better.

Change-Id: I88342f79c9930c81cd90974904d014bf3fc3f73a
---
M src/LdapGroups.php
1 file changed, 57 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/LdapGroups 
refs/changes/99/348099/1

diff --git a/src/LdapGroups.php b/src/LdapGroups.php
index 4a7b92b..812f6d9 100644
--- a/src/LdapGroups.php
+++ b/src/LdapGroups.php
@@ -1,4 +1,20 @@
 <?php
+/*
+ * Copyright (C) 2017  Mark A. Hershberger
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
 
 namespace LdapGroups;
 
@@ -15,21 +31,7 @@
 
        public function __construct( $param ) {
                wfDebug( __METHOD__ );
-               $this->ldap = ldap_connect( $param['server'] );
-               if ( !$this->ldap ) {
-                       throw new MWException( "Error Connecting to LDAP 
server.!" );
-               }
                $this->param = $param;
-
-               ldap_set_option( $this->ldap, LDAP_OPT_REFERRALS, 0 );
-               ldap_set_option( $this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3 );
-               if ( !ldap_bind( $this->ldap, $this->param['user'],
-                                                $this->param['pass'] )
-               ) {
-                       throw new MWException( "Couldn't bind to LDAP server: " 
.
-                                                                  ldap_error( 
$this->ldap ) );
-               }
-
                $this->setupGroupMap();
        }
 
@@ -106,21 +108,50 @@
                $this->setGroupRestrictions( $groupMap );
        }
 
-       protected function doLDAPSearch( $match ) {
-               wfProfileIn( __METHOD__ . " - LDAP Search" );
-               $runTime = -microtime( true );
-               $res = ldap_search( $this->ldap, $this->param['basedn'],
-                                                       $match, [ "*" ] );
-               if ( !$res ) {
-                       wfProfileOut( __METHOD__ );
-                       throw new MWException( "Error in LDAP search: " .
+    protected function setupConnection() {
+               $this->ldap = ldap_connect( $this->param['server'] );
+               if ( !$this->ldap ) {
+                       throw new MWException( "Error Connecting to LDAP 
server!" );
+               }
+               ldap_set_option( $this->ldap, LDAP_OPT_REFERRALS, 0 );
+               ldap_set_option( $this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3 );
+               if ( !ldap_bind( $this->ldap, $this->param['user'],
+                                                $this->param['pass'] )
+               ) {
+                       throw new MWException( "Couldn't bind to LDAP server: " 
.
                                                                   ldap_error( 
$this->ldap ) );
                }
+    }
 
-               $entry = ldap_get_entries( $this->ldap, $res );
-               $runTime += microtime( true );
-               wfProfileOut( __METHOD__ . " - LDAP Search" );
-               wfDebugLog( __CLASS__, "Ran LDAP search for '$match' in 
$runTime seconds.\n" );
+       protected function doLDAPSearch( $match ) {
+               wfProfileIn( __METHOD__ );
+               $runTime = -microtime( true );
+        $key = wfMemcKey( 'ldapgroups', $match );
+        $cache = wfGetMainCache();
+        $entry = $cache->get( $key );
+        if ( $entry === false ) {
+            wfProfileIn( __METHOD__ . " - LDAP Search" );
+            if ( !$this->ldap ) {
+                $this->setupConnection();
+            }
+
+            $res = ldap_search( $this->ldap, $this->param['basedn'],
+                                $match, [ "*" ] );
+            if ( !$res ) {
+                wfProfileOut( __METHOD__ . " - LDAP Search" );
+                wfProfileOut( __METHOD__ );
+                throw new MWException( "Error in LDAP search: " .
+                                       ldap_error( $this->ldap ) );
+            }
+
+            $entry = ldap_get_entries( $this->ldap, $res );
+            $cache->set( $key, $entry, 3600 * 24 );
+
+            wfProfileOut( __METHOD__ . " - LDAP Search" );
+        }
+        wfProfileOut( __METHOD__ );
+        $runTime += microtime( true );
+        wfDebugLog( __CLASS__, "Ran LDAP search for '$match' in $runTime 
seconds.\n" );
                return $entry;
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/348099
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I88342f79c9930c81cd90974904d014bf3fc3f73a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/LdapGroups
Gerrit-Branch: master
Gerrit-Owner: MarkAHershberger <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to