連線快取
建立與伺服器的連線時,連接句柄會在用戶端電腦上快取該進程,直到該連接關閉為止。 如果後續連接使用相同的伺服器、埠和認證,而且只有 ADS_FAST_BIND 或 ADS_SERVER_BIND 驗證旗標不同,ADSI 會重複使用現有的連線。 ADSI 會根據每個進程執行此連線快取。
若要提高效能,請盡可能重複使用現有的連線。
以下是在 Visual Basic 中使用 IAD 的範例(另請參閱 使用 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.
下列替代範例顯示連線快取如何使用 .NET 中的 DirectoryEntry 物件運作:
// 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