身份相关实体的高效缓存 [英] Efficient caching of Identity related entities

查看:82
本文介绍了身份相关实体的高效缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net MVC5和asp.net Identity v2(从每晚发布的版本),但我认为此问题仍然适用于Identity V1.

I'm using asp.net MVC5 and asp.net Identity v2 (from the nightly releases) but I think this question still applies to Identity V1.

我有一个会员制,我正在通过membershipuser表中的字段AspUserIdAspNetUser实体链接到我的membershipuser实体.

I have a membership system and i am linking a AspNetUser entity to my membershipuser entity via a field AspUserId in the membershipuser table.

public partial class membershipuser
{
    public int id { get; set; }
    public string full_name { get; set; }
    public string address { get; set; }
    public string AspNetUserId { get; set; }
    .....
}

我想知道在请求有效期内缓存您的MembershipUser记录的最佳方法是什么.这样:

I was wondering what is the best method of caching your membershipuser record for the life of the request. This way:

public static class extensions
    {
        public static membershipuser GetMember(this System.Security.Principal.IPrincipal User)
        {
            string currentUserId = User.Identity.GetUserId();
            return new MemberEntities().membershipusers.FirstOrDefault(m => m.AspNetUserId == currentUserId );
        }
    }

或者这样:

 public abstract partial class BaseController : Controller
{
    private membershipuser _Member;

    public membershipuser Member
    {
        get { 
            if(_BASRaTMember == null)
            {
               string currentUserId = User.Identity.GetUserId(); 
                BASRaTMember = new BasratEntities().tbl_basrat_members.FirstOrDefault(m => m.AspNetUserId == currentUserId );
            }
            return _BASRaTMember; 
        }
        private set { _BASRaTMember = value; }
    }

}

我认为Basecontroller方法是最好的,因为我猜测如果我调用扩展方法5次以检查成员属性,它将为用户查询数据库5次.还是最好以某种方式针对请求存储它?我唯一的意图是减少对用户进行身份验证后对请求的数据库查找.

I'm thinking the Basecontroller method is best as I'm guessing if I called the extension method 5 times to check member properties it would query the database for the user 5 times. Or would it be better to somehow store it against the request? My sole intention is to reduce the database lookups for the requests after the user has been authenticated.

推荐答案

因此,如果您遵循每个请求(OwinContext)使用单个DbContext的示例模式,因为DbContext进行了这种缓存,那么在2.0中这将不再是问题.本身.但是,如果想要不管特定的存储缓​​存行为如何保证这种缓存,也可以通过用户ID显式缓存用户,则需要使用某种帮助程序方法,而不是直接使用用户管理器.辅助程序会负责填充缓存,即类似

So this is less of an issue in 2.0 if you follow the samples pattern of using a single DbContext per request(OwinContext) as the DbContext does this kind of caching itself. But you could explicitly cache users by their UserId as well if you wanted to guarantee this caching irrespective of the specific store caching behavior, you would need to go through some kind of helper method instead of directly using the user manager. The helper would take care of populating the cache, i.e. something like this

public async Task<TUser> GetUser(this IOwinContext context, TKey userId, UserManager<TUser, TKey> manager) {
      var contextKey = "computeYourCacheKey"+userId;
      var user = context.Get<TUser>(contextKey);
      if (user == null) {
          user = await manager.FindByIdAsync(userId);
          context.Set<TUser>(userId, user);
      }
      return user;
}

这篇关于身份相关实体的高效缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆