System.Web.HttpContext.Current.Cache.Add()的方法有一个参数为System.Web.Caching.CacheDependency
MSDN上说明:
public Object Add (
string key,
Object value,
CacheDependency dependencies,
DateTime absoluteExpiration,
TimeSpan slidingExpiration,
CacheItemPriority priority,
CacheItemRemovedCallback onRemoveCallback
)
参数
dependencies
该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含 空引用(在 Visual Basic 中为 Nothing)。
意思是,如果缓存依赖于某一个物理文件,而该物理文件在缓存的有效期内被更改的话,那么缓存也会失效。现在我却被这个依赖弄糊涂了
if (System.Web.HttpContext.Current.Cache["PPSiteConfig"] == null)
{
System.Collections.Hashtable ht = new System.Collections.Hashtable();
XmlDocument doc = new XmlDocument();
doc.Load(ConfigFile);
foreach (XmlNode node in doc.ChildNodes[1].ChildNodes)
{
if (ht.ContainsKey(node.Name) == false)
ht.Add(node.Name, node.InnerText);
}
System.Web.HttpContext.Current.Cache.Add("PPSiteConfig", ht,
new System.Web.Caching.CacheDependency(ConfigFile), DateTime.Now.AddMinutes(30), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
}
Hashtable ht_config = HttpContext.Current.Cache["PPSiteConfig"] as Hashtable;
很明显,Key为"PPSiteConfig"的缓存依融于ConfigFile文件,现在遇到的情况是,当我手工更改了该文件,但缓存却还是更改前的内容,也就是说CacheDependency并未生效,晕了.....