0

我正在使用 asp.net Sqlmembership.GetAll()方法(分页重载)。现在我想添加一个缓存层来缓存结果,但是 GetAll 方法的out 参数存在问题,它返回记录总数。从缓存中检索数据时,如何为totalRecords参数赋值?

4

1 回答 1

0

如果我的理解是正确的,这个小流程将帮助您实现目标

这是一个基本流程:

  1. 当你想访问缓存的对象时,向缓存提供者询问
  2. 如果对象不为空,则转换为正确的类型并从缓存中返回对象(在本例中为用户列表)。结束进程
  3. 如果对象为空,则
    1. 从原始来源检索对象(使用GetAll方法)
    2. 将检索到的对象保存到缓存中
    3. 返回检索到的对象。结束进程

无论如何,我建议您使用自定义域类而不是MembershipUser

这是一个基本示例:

    public IEnumerable<DomainUser> GetDomainUsers()
    {
        var context = HttpContext.Current;
        var cache = context.Cache;
        var domainUsers = cache["domainUsers"] as IEnumerable<DomainUser>;

        if (domainUsers == null)
        {
            domainUsers = Membership.GetAllUsers().OfType<MembershipUser>().Select(x => new DomainUser
                {
                    Email = x.Email,
                    Username = x.UserName
                });

            cache.Insert(
                "domainUsers", // cache key
                domainUsers, // object to cache
                null, // dependencies
                DateTime.Now.AddMinutes(30), // absoulute expiration
                Cache.NoSlidingExpiration, // slading expiration
                CacheItemPriority.High, // cache item priority
                null // callback called when the cache item is removed
            );

            context.Trace.Warn("Data retrieved from its original source");
        }
        else
        {
            context.Trace.Warn("Data retrieved from cache");

        }

        return domainUsers;
    }
于 2012-09-15T07:46:50.563 回答