我在网上找到了一些代码,让我感到困惑。请看下面的代码。你会注意到只有当 Hits == 1 时,缓存才会被添加。之后,缓存对象就不再更新了。这引发了一个问题,当对象更新时,缓存是否也会自动更新?这里的答案将使我删除一些类中的代码。
我猜我需要在if语句后面添加这行代码:
该链接提供了与 ASP.NET 性能相关的代码。
public static bool IsValid( ActionTypeEnum actionType )
{
HttpContext context = HttpContext.Current;
if( context.Request.Browser.Crawler ) return false;
string key = actionType.ToString() + context.Request.UserHostAddress;
var hit = (HitInfo)(context.Cache[key] ?? new HitInfo());
if( hit.Hits > (int)actionType ) return false;
else hit.Hits ++;
if( hit.Hits == 1 )
context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(DURATION),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal, null);
return true;
}
我猜我需要在if语句后面添加这行代码:
if( hit.Hits == 1 )
context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(10),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal, null);
else if (hit.Hits > 1)
{context.Cache.Remove(key);
context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(10),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal, null);
}
在这里找到了底部的代码:http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx?msg=2809164
该链接提供了与 ASP.NET 性能相关的代码。