Clearing Cache Objects Upon Page Unload

Last post 05-18-2008 10:46 AM by NC01. 8 replies.

Sort Posts:

  • Clearing Cache Objects Upon Page Unload

    05-16-2008, 8:19 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 878

    I have a web page in which a lot of editing is done.  To accomplish what the user requires I implemented a number of cached objects, mostly DataTables.  It all works fine.  But . . . when the user leaves the page I'd like to clear them all.

    In terms of leaving the page, I've implemented this Javascript functionality:

       window.onbeforeunload = ConfirmExit;

        function ConfirmExit()
        {
          needToConfirmExit = Boolean(document.getElementById('<%= needToSaveData.ClientID %>').value);
         
          if (needToConfirmExit)
            return "You have made changes that have not yet been saved back to the database.  If you leave now, all of those changes will be lost.  Are you absolutely sure you want to do this?";
        }

    So if the user does decide to leave the page then in the "onunload" event I could potentially clear the Cached objects.  But they're ASP.Net objects so how to I access them from Javascript?

    Robert W. 

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Clearing Cache Objects Upon Page Unload

    05-17-2008, 12:14 AM
    • Participant
      1,238 point Participant
    • jchandra
    • Member since 05-15-2008, 9:36 AM
    • Jakarta, Indonesia
    • Posts 197

     Since I don't know the entire design, I'd have to say that I'm not sure if this is the correct usage for Cache object.  You can use ViewState which is tightly coupled to the Page itself.  Cache should be used for things that stay longer than just a lifetime of a Page interaction I think.

    In any case, you can choose to do a window.open in the window.onbeforeunload event to a cache cleaner utility page that will close itself once it's done

    or you can do something different like detecting the page url and it's referrer to see if it's coming from the same page on the destination page like so... 

     

    1        if (Request.UrlReferrer != null)
    2            {
    3                if (Request.UrlReferrer.AbsolutePath != Request.Url.AbsolutePath)
    4                {
    5                    Response.Write("I am coming from a different page, clear the cache");
    6                }
    7                else
    8                {
    9                    Response.Write("I am coming from the same page, do nothing");
    10               }
    11           }
    
      
    Jimmy Chandra
    Blogging at Incoherent Rambling

    Mark this post as Answer if you think it helped you solve the problem.

  • Re: Clearing Cache Objects Upon Page Unload

    05-17-2008, 12:23 AM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 878

    Jimmy,

    Thanks for your thoughts.  However, there's a golden rule about now overusing ViewState, since everything you add to it will be attached to each & every page postback.  Storing DataTables (especially larger ones) in ViewState is not advisable.

    I'm actually seeking to find out the syntax for accessing the Cache variables from the client.

    Robert
     

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Clearing Cache Objects Upon Page Unload

    05-17-2008, 12:32 AM
    • All-Star
      20,674 point All-Star
    • A1ien51
    • Member since 05-06-2005, 6:46 PM
    • MD USA
    • Posts 3,803
    The client and the server are separate. You can access the server directly. Only option would be to use Aajx or rely on session timeout. Eric
  • Re: Clearing Cache Objects Upon Page Unload

    05-17-2008, 7:18 AM
    • Participant
      1,238 point Participant
    • jchandra
    • Member since 05-15-2008, 9:36 AM
    • Jakarta, Indonesia
    • Posts 197

    Like Alien51 said... Ajax could be your best buddy in this type of situation.

    One example (if you are using ASP.NET AJAX) to do this can be found here.

    Jimmy Chandra
    Blogging at Incoherent Rambling

    Mark this post as Answer if you think it helped you solve the problem.

  • Re: Clearing Cache Objects Upon Page Unload

    05-17-2008, 6:27 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 878

    Eric & Jimmy:

    Thank you for your last comments.  Based on what you said I re-investigated PageMethods.  I'd done this before in some example code and tried to implement it here.  I got everything wired up correctly but then ran into "static vs. non-static" problem.  I don't 100% understand it but essentially my belief is that PageMethods only work with static items whereas a web page and the cache is a non-static instantiated items.  The error I received when trying to clear a cache item was this:

         Error    7    An object reference is required for the nonstatic field, method, or property 'System.Web.Caching.Cache.Remove(string)'

    Whether there's a way around this, I do not know.  Perhaps someone will come across this posting who has been able to successfully tap into Web Page objects from PageMethods.  I haven't.

    Robert 

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Clearing Cache Objects Upon Page Unload

    05-17-2008, 9:09 PM
    Answer
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 878

    Thanks for your help, gents!  You got me pointed in the right direction and I've now solved it!  I wrote all about the solution on my technical blog.

    Robert 

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Clearing Cache Objects Upon Page Unload

    05-18-2008, 3:43 AM
    • Participant
      1,238 point Participant
    • jchandra
    • Member since 05-15-2008, 9:36 AM
    • Jakarta, Indonesia
    • Posts 197

    Use HttpContext.Current.Cache instead since you won't have access to instance members of Page object.  They are basically the same thing.  You will need to do this anyhow when you are trying to access Cache / Response / Request, etc. from your own webservice / class library, no different here.

    Be careful when doing this however.  Ran into a nasty bug last time when I tried to cache the Request object in my own helper library static object.  Blogged about it here.

    Jimmy Chandra
    Blogging at Incoherent Rambling

    Mark this post as Answer if you think it helped you solve the problem.

  • Re: Clearing Cache Objects Upon Page Unload

    05-18-2008, 10:46 AM
    • All-Star
      76,169 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,181

    You would probably be better to be using Session objects instead of Cache objects in this instance. Session objects are tied to each user session while Cache objects are tied to the server and application itself. Session objects also get wiped after a designated time of no user input (which you can set yourself), so you would not have to really do anything to clear them.

    NC...

Page 1 of 1 (9 items)