首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >缓存Active Directory数据

缓存Active Directory数据
EN

Stack Overflow用户
提问于 2011-07-06 00:42:49
回答 1查看 1.3K关注 0票数 2

我想每天缓存两次active directory数据,即电子邮件、名字和姓氏。这应该在幕后发生,可能是在global.aspx application.start()中。现在,当And我想要获取1700个用户(我的应用程序允许浏览用户的功能)时,我会从缓存数据中获取。我的代码在Asp.net C#中。

有人能告诉我怎么做吗?

EN

Stack Overflow用户

回答已采纳

发布于 2011-07-06 21:16:44

我的代码在第一次被访问时运行(您可以通过在global.asax文件中放置几行代码来启动此应用程序)。它将缓存的数据在ASP.Net缓存中存储12小时。12小时后,缓存将在下一次访问数据时刷新。

代码语言:javascript
运行
复制
public class UserInfo
{
    public static List<UserInfo> UserInfoCache { 
        get{
            if(HttpContext.Current.Cache["CachedUserList"] == null) {
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

                PrincipalSearcher ps = new PrincipalSearcher(new UserPrincipal(ctx) { SamAccountName = "*" });

                var users = ps.FindAll();

                var result = from c in users
                             let u = (UserPrincipal)c
                             select new UserInfo() { Email = u.EmailAddress, FirstName = u.GivenName, LastName = u.Surname };

                HttpContext.Current.Cache.Insert("CachedUserList", result, null, DateTime.Now.AddHours(12), System.Web.Caching.Cache.NoSlidingExpiration);
            }

            return (List<UserInfo>)HttpContext.Current.Cache["CachedUserList"];
        }
    }

    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
票数 6
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6586012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档