CacheDependency with File Dependency

Discussion in 'ASP .Net' started by TJO, Dec 8, 2005.

  1. TJO

    TJO Guest

    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?
     
    TJO, Dec 8, 2005
    #1
    1. Advertisements

  2. TJO

    Bruce Barker Guest

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

    -- bruce (sqlwork.com)
     
    Bruce Barker, Dec 8, 2005
    #2
    1. Advertisements

  3. TJO

    TJO Guest

    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;
    }
     
    TJO, Dec 8, 2005
    #3
  4. TJO

    TJO Guest

    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;
    }
     
    TJO, Dec 8, 2005
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.