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. Advertising

  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)


    "TJO" <> wrote in message
    news:...
    > 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?
    >
    Bruce Barker, Dec 8, 2005
    #2
    1. Advertising

  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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith
    Replies:
    0
    Views:
    527
    Keith
    Oct 1, 2003
  2. Brian

    CacheDependency

    Brian, Dec 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    557
    Brian
    Dec 20, 2003
  3. Dave S

    cachedependency not working ?

    Dave S, Jul 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    789
    Dave S
    Jul 21, 2004
  4. Weston Weems
    Replies:
    0
    Views:
    445
    Weston Weems
    May 27, 2005
  5. Keith
    Replies:
    2
    Views:
    155
    Keith
    Oct 2, 2003
Loading...

Share This Page