我在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)