CacheDependency with File Dependency

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?
 
B

Bruce Barker

with no sample code, its hard to know what you did wrong. the file
dependency works fine (does require ntfs filesystem).

-- bruce (sqlwork.com)
 
T

TJO

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;
}
 
T

TJO

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top