小编Big*_*ike的帖子

添加对象后,MemoryCache为空

我在MVC 3 ASP.NET应用程序中遇到了一个奇怪的ASP.NET MemoryCaching问题.

每次执行一个动作时,我都会检查它的LoginInfo是否实际存储在MemoryCache中(代码已经简化,但核心如下):

[NonAction]
protected override void OnAuthorization(AuthorizationContext filterContext) {
  Boolean autorizzato = false;
  LoginInfo me = CacheUtils.GetLoginData(User.Identity.Name);
  if (me == null)
  {
    me = LoginData.UserLogin(User.Identity.Name);
    CacheUtils.SetLoginInfo(User.Identity.Name, me);
  }
  // Test if the object is really in the memory cache
  if (CacheUtils.GetLoginData(User.Identity.Name) == null) {
     throw new Exception("IMPOSSIBLE");
  } 
}
Run Code Online (Sandbox Code Playgroud)

GetLoginInfo是:

 public static LoginInfo GetLoginData(String Username)
        {
            LoginInfo local = null;
            ObjectCache cache = MemoryCache.Default;
            if (cache.Contains(Username.ToUpper()))
            {
                local = (LoginInfo)cache.Get(Username.ToUpper());
            }
            else
            {
                log.Warn("User " + Username …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc c#-4.0

8
推荐指数
1
解决办法
4196
查看次数

标签 统计

asp.net-mvc ×1

c#-4.0 ×1