的MemoryCache添加对象空刚过 [英] MemoryCache empty just after adding an object

查看:145
本文介绍了的MemoryCache添加对象空刚过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临与ASP.NET MemoryCaching一个奇怪的问题在MVC 3 ASP.NET应用程序。

I'm facing a strange problem with ASP.NET MemoryCaching in a MVC 3 ASP.NET application.

每个执行一个动作的时候,我检查,如果其将LoginInfo实际上是存储在的MemoryCache(code已被简化,但核心是如下):

Each time an action is executed, I check if its LoginInfo are actually stored in the MemoryCache (code has been simplified, but core is as follow):

[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");
  } 
}

该GetLoginInfo是:

The GetLoginInfo is:

 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 + " not found  in cache");
            }
            return local;
        }

该SetLoginInfo是:

The SetLoginInfo is:

        public static void SetLoginInfo (String Username, LoginInfo Info)
        {
            ObjectCache cache = MemoryCache.Default;
            if ((Username != null) && (Info != null))
            {
                if (cache.Contains(Username.ToUpper()))
                {
                    cache.Remove(Username.ToUpper());
                }
                cache.Add(Username.ToUpper(), Info, new CacheItemPolicy());
            }
            else
            {
                log.Error("NotFound...");
            }
       }

在code是pretty简单,但有时(完全随机),只需添加将LoginInfo到的MemoryCache后,这将导致空的,只是增加了对象不是present,因此我得到了例外。

The code is pretty straightforward, but sometimes (totally randomly), just after adding the LoginInfo to the MemoryCache, this results empty, the just added Object is not present, therefore I got the Exception.

我测试这无论在卡西尼和IIS 7,似乎不相关的应用程序池可重用性(在IIS 7中启用),我和几个缓存策略进行测试,但不能使它工作。

I'm testing this both on Cassini and IIS 7, it seems not related to AppPool reusability (enabled in IIS 7), I've tested with several Caching policies, but cannot make it work

我缺少/失败?

PS:原谅我的英语不好

PS: forgive me for my bad english

推荐答案

我相信你正在运行到斯科特Hanselman的确定为.NET 4的bug问题。请在这里看到:<一href=\"http://stackoverflow.com/questions/7422859/memorycache-empty-disposed-in-web-application\">MemoryCache空:设置在Web应用程序中

I believe you are running into a problem that Scott Hanselman identified as a .NET 4 bug. Please see here: MemoryCache Empty : Disposed in Web application

这篇关于的MemoryCache添加对象空刚过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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