我想为某些键存储空值,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);
}
最后一行代码,即 HttpRuntime.Cache.Add 抛出 System.ArgumentNullException: Value cannot be null。
知道这是否可能,或者我需要使用其他数据结构来存储空值并稍后查找?