我正在尝试提高对活动目录分层组结构的 searcher.FindAll() 调用的性能。
该代码获取所有用户组的 SearchResultCollection,然后根据搜索字符串过滤组。SearchResultCollection 集合中有大约 400 个组,过滤后的版本通常达到 40 个。在迭代调用期间,我没有看到内置缓存的任何好处(DirectorySearcher.CacheResults = True)。
例如,如果我连续调用下面的代码 20 次,执行时间几乎相同。
- 我已经看到此链接DirectorySearcher Filter并了解 DistinguishedName 不可用于 Searcher.Filter 属性中的通配符搜索(我已通过测试确认,我希望 DirectorySearcher 处理过滤)
- 默认情况下,DirectorySearcher 对象已启用缓存,但在这里似乎根本没有帮助(我将其设置为 True 无论如何)
- 文档指出,如果结果很大,DirectorySearcher 将决定不缓存,知道这个阈值是多少吗?
下面的代码每次执行 SearchResult 的所有 400 次迭代都会受到相同的惩罚。
string groupFilter = "ExampleFilter";
DirectoryEntry root = GetRootDirectoryEntry();
string userPath = GetUserPath(username, root);
var searcher = new DirectorySearcher(root)
{
SearchScope = SearchScope.Subtree,
Filter = string.Format("(member:XXXX:={0})", userPath) + ""
//Any wildcard based search for distinguishedname like below returns no results, which is incorrect
//Filter = string.Format("(& (member:XXXX:={0}) (distinguishedname=*{1}) )", userPath, groupFilter)
};
searcher.PropertiesToLoad.Add("Name");
searcher.PropertiesToLoad.Add("distinguishedname");
var groupResults = searcher.FindAll();
var allFilteredGroups = new HashSet<string>();
foreach (SearchResult gr in groupResults) //approx 400 in the groupResults, same time penalty for every iteration, every time
{
var isRelevant = gr.Path.Contains(groupFilter);
if (isRelevant)
{
//Do Stuff
allFilteredGroups.Add(value);
}
}
return allFilteredGroups.ToList(); //approx 40
为什么缓存没有提供任何改进?关于如何减少迭代所有这些我知道我不想要的组的惩罚的任何建议?