CacheDependency with File Dependency

  • Thread starter Thread starter TJO
  • Start date Start date
T

TJO

My asp.net 1.1 app is loading an xml file into cache so that subsequent
calls for the xml file will not be pulled from the file system but from
the cache instead.

My method for loading the xml does the following:

1) Checks the cache to see if it is there
2) If not in cache load xml file, else pull it from cache
3) Creates new CacheDependency object using the full path of the xml
and current datetime
3) Inserts xml into cache using the CacheDependency object

This is working fine but when I change the xml file the application
does not reload the file but keeps pulling it from cache!

Why does it not recognize my file being changed?
 
with no sample code, its hard to know what you did wrong. the file
dependency works fine (does require ntfs filesystem).

-- bruce (sqlwork.com)
 
Here you go:
public static XmlDocument _GetMenuXML()
{
string _path =
HttpContext.Current.Server.MapPath("./xml/Menu.xml");

XmlDocument _xmlDoc = new XmlDocument();

// Get Menu.xml from cache or file
if(HttpContext.Current.Cache["MainMenuXML"] == null)
{
_xmlDoc.Load(_path);
HttpContext.Current.Cache["MainMenuXML"] = _xmlDoc;

// Cache is dependent upon changes to the Menu.xml file
CacheDependency cd = new CacheDependency(_path,DateTime.Now);

// Add Menu.xml to cache
HttpContext.Current.Cache.Insert(
"MainMenuXml",
_xmlDoc,
cd);
}
else
{
_xmlDoc = (XmlDocument)HttpContext.Current.Cache["MainMenuXML"];
}

return _xmlDoc;
}
 
Previous code had error but now the cache evaluation is always false.


public static XmlDocument _GetMenuXML()
{
string _path =
HttpContext.Current.Server.MapPath("./xml/Menu.xml");

XmlDocument _xmlDoc = new XmlDocument();

// Get Menu.xml from cache or file
if(HttpContext.Current.Cache.Get("MainMenuXML") == null)
{
_xmlDoc.Load(_path);

// Cache is dependent upon changes to the Menu.xml file
CacheDependency cd = new CacheDependency(_path);

// Add Menu.xml to cache
HttpContext.Current.Cache.Insert(
"MainMenuXml",
_xmlDoc,
cd);
}
else
{
_xmlDoc = (XmlDocument)HttpContext.Current.Cache["MainMenuXML"];
}

return _xmlDoc;
}
 
Back
Top