Cache de conexão
Quando uma conexão com um servidor é feita, o identificador de conexão é armazenado em cache no computador cliente para esse processo até que essa conexão seja fechada. Se o mesmo servidor, porta e credenciais forem usados em uma conexão subsequente e apenas os sinalizadores de autenticação ADS_FAST_BIND ou ADS_SERVER_BIND forem diferentes, o ADSI reutilizará a conexão existente. O ADSI executa esse cache de conexão por processo.
Para aumentar o desempenho, reutilize as conexões existentes quando possível.
A seguir está um exemplo usando IADs no Visual Basic (consulte também Usando as interfaces IADs):
Dim cachedConn As IADs
Dim obj As IADs
Dim cachedName As String
Dim objName As String
' Connect to the server and maintain this handle to cache the connection.
Set cachedConn = GetObject("LDAP://MyMachine/DC=MyDomain,DC=Fabrikam,DC=com")
cachedName = cachedConn.Get("distinguishedName")
Debug.Print (cachedName)
' Reuse the connection to MyMachine opened by cachedConn.
' Be aware that this line executes quickly because it is not required
' to transmit over the network again.
Set obj = GetObject("LDAP://MyMachine/CN=Bob,CN=Users,DC=MyDomain,DC=Fabrikam,DC=com")
objName = obj.Get("distinguishedName")
Debug.Print (objName)
' Release the second connection.
Set obj = Nothing
' Reuse the connection to MyMachine opened by cachedConn again.
Set obj = GetObject("LDAP://MyMachine/CN=Administrator,CN=Users,DC=MyDomain,DC=Fabrikam,DC=com")
objName = obj.Get("distinguishedName")
Debug.Print (objName)
' Release the second connection again.
Set obj = Nothing
' Release the first connection.
Set cachedConn = Nothing
' The connection to MyMachine is closed.
O exemplo alternativo a seguir mostra como o cache de conexão funciona usando o objeto DirectoryEntry no .NET:
// Connect to the server and maintain this handle to cache the connection.
using (DirectoryEntry? cachedConn = new DirectoryEntry("LDAP://MyMachine/DC=MyDomain,DC=Fabrikam,DC=com"))
{
DirectoryEntry? secondConn;
string? cachedName;
string? objName;
cachedName = cachedConn.Properties["distinguishedName"].Value?.ToString();
Debug.WriteLine(cachedName);
// Reuse the connection to MyMachine opened by cachedConn.
// Be aware that this line executes quickly because it is not required
// to transmit over the network again.
using (secondConn = new DirectoryEntry("LDAP://MyMachine/CN=Bob,CN=Users,DC=MyDomain,DC=Fabrikam,DC=com"))
{
objName = secondConn.Properties["distinguishedName"].Value?.ToString();
Debug.WriteLine(objName);
// Release the second connection.
secondConn = null;
// Reuse the connection to MyMachine opened by cachedConn again.
secondConn = new DirectoryEntry("LDAP://MyMachine/CN=Administrator,CN=Users,DC=MyDomain,DC=Fabrikam,DC=com");
objName = secondConn.Properties["distinguishedName"].Value?.ToString();
Debug.WriteLine(objName);
}
// Release and dispose the second connection
}
// The connection to MyMachine is closed and disposed