我是asp.net的初学者,并且有一些关于Cache的问题:
我知道大多数人都建议使用HttpRuntime.Cache,因为它具有更大的灵活性......等等.但是如果你希望对象在应用程序的生命周期中保留在缓存中呢?使用Application []对象缓存内容有什么大的缺点吗?
在过去,我已经锁定了访问HttpRuntime.Cache机制.我不确定我过去是否真的研究过这个问题,盲人用锁把它围起来.
你觉得这真的有必要吗?
我知道这里有一个非常相似的问题,但我希望能有更好的探索.如果HttpContext真的在后台使用HttpRuntime.Cache,为什么我会使用HttpContext.Cache而不是HttpRuntime.Cache?
在使用ASP.NET模拟Windows服务来运行预定作业的文章中, Omar使用HttpContext来存储他的缓存项,但是当Jeff Atwood 在这里实现它时,他选择使用HttpRuntime.显然,在这种特殊情况下,它是有意义的,因为您不必执行Web请求将缓存项添加回HttpContext.
但是,我正在寻找一些关于何时使用一个与另一个的好指针.
这是我见过的最奇怪的错误之一.
我正在做一个非常简单的调用来从HttpRuntime缓存中返回值.电话是:
return HttpContext.Current.Cache[cacheKey];
Run Code Online (Sandbox Code Playgroud)
如果它返回null,那很好.我检查返回的值是否为null并相应地采取行动.我一直在使用这个电话.
最近,出于某种原因,当cacheKey设置为这个确切的值时:
"Topic_GridSelectAll:5,null,2010-08-31-20-00-00,Published,desc,5,1"
Run Code Online (Sandbox Code Playgroud)
抛出System.OverflowException:否定二进制补码数的最小值无效.
呼叫,相关代码或服务器没有任何变化.如果cacheKey的字符略有不同,那么它的工作原理非常好.例如,此cacheKey返回null而不抛出任何异常:
"Topic_GridSelectAll:5,null,2010-08-31-21-00-00,Published,desc,5,1"
Run Code Online (Sandbox Code Playgroud)
请注意,这两个字符串之间的唯一区别是时间字符:2010-08-31-20-00-00与2010-08-31-21-00-00.
为什么这会有什么不同呢?为什么现在这么久以后呢?
堆栈跟踪是:
[OverflowException: Negating the minimum value of a twos complement number is invalid.]
System.Math.AbsHelper(Int32 value) +12753486
System.Web.Caching.CacheMultiple.UpdateCache(CacheKey cacheKey, CacheEntry newEntry, Boolean replace, CacheItemRemovedReason removedReason, Object& valueOld) +142
System.Web.Caching.CacheInternal.DoGet(Boolean isPublic, String key, CacheGetOptions getOptions) +122
MyProject.Helpers.CacheHelper.GetData(String cacheDomain, String cacheKey) in ...
Run Code Online (Sandbox Code Playgroud)
我已经尝试更改缓存调用以使用HttpRuntime.Cache(即.HttpRuntime.Cache[cacheKey]
),但这没有任何区别.我知道它是相同的底层缓存提供者,但我想也许不同的调用会产生影响.没有骰子.
是否有任何工具可用于查看HttpRunTime缓存中的缓存数据..?
我们有一个Asp.Net应用程序,它将数据缓存到HttpRuntime Cache中.给定的默认值为60秒,但后来更改为5分钟.但感觉缓存的数据在5分钟之前就会刷新.不知道底下发生了什么.
有没有可用的工具或我们如何看到HttpRunTime Cache中缓存的数据....有效期...?
以下代码用于添加要缓存的项目.
public static void Add(string pName, object pValue)
{
int cacheExpiry= int.TryParse(System.Configuration.ConfigurationManager.AppSettings["CacheExpirationInSec"], out cacheExpiry)?cacheExpiry:60;
System.Web.HttpRuntime.Cache.Add(pName, pValue, null, DateTime.Now.AddSeconds(cacheExpiry), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我使用a HttpRuntime.Cache
来存储将在会话中频繁访问的对象列表.
我使用以下代码行从缓存中获取项目:
List<chartData_Type> _chartData =
(List<chartData_Type>)HttpRuntime.Cache.Get("rollingMonth");
Run Code Online (Sandbox Code Playgroud)
但是,不幸的是,当我更新_chartData时,它也会更新缓存的项目.
我怎样才能简单地获得缓存项目的副本?
我目前在ASP.NET HttpRuntime.Cache中存储了许多不同类型的对象,我想知道是否有办法弄清楚每个对象有多大?
我想为某些键存储null,HttpRuntime.Cache
因为我不想再次转到数据库以找到该键没有条目.
所以第一次,它进入数据库并填充缓存.目的是使用缓存数据而不是进行数据库调用来提供以下调用.
这是我使用以下代码:
Info info = null;
if (HttpRuntime.Cache["Info_" + id.ToString() + "_" + quantity.ToString()] != null)
info = HttpRuntime.Cache["Info_" + id.ToString() + "_" + quantity.ToString()] as Info;
if (info == null)
{
info = (from dd in dc.Infos
where dd.id == id && dd.active == true && dd.quantitytooffset == quantity
select dd).SingleOrDefault();
HttpRuntime.Cache.Add("Info_" + id.ToString() + "_" + quantity.ToString(), info, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
}
Run Code Online (Sandbox Code Playgroud)
最后一行代码即HttpRuntime.Cache.Add抛出一个System.ArgumentNullException:Value不能为null.
任何想法是否可行或我需要使用其他数据结构来存储空值并在以后查找?
我们可以在HttpRuntime Cache中为一个项目设置的最大到期时间是多少?
另外,默认的到期时间是多少?
public static void Add(string pName, object pValue)
{
System.Web.HttpRuntime.Cache.Add(pName, pValue, null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,第4个参数是'absoluteExpiration'。
我们在这里可以提供的最大价值是多少...?
如果我提供2014年5月5日,该项目可以在高速缓存中使用很长时间吗?
(此查询与AppFabric缓存的实现有关。尝试用AppFabric缓存替换Httpruntime缓存)。