Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / December 2003

Tip: Looking for answers? Try searching our database.

Session_End() and the Cache

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Breck - 14 Nov 2003 16:04 GMT
[VB.NET]

When a user's Session expires, I'd like to remove certain things from
the Cache.  For instance, some data items I've stored using the user's
SessionID, so using that, I could remove some of these things from the
Cache...

...if I could get to it.

I know how to iterate the Cache, but the Context object is Nothing
when the Session_End() event fires!  Take a look at my code:

  '-------------------------------------------------------------------------
  '   Session_End()
  '-------------------------------------------------------------------------
  Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)

     Try
        Dim oIEnumerator As IDictionaryEnumerator
        Dim oDictionaryEntry As System.Collections.DictionaryEntry
        Dim sKey As String
        Dim sKeyPrefix As String = Me.Session.SessionID & "."
        'Dim sPrefixes() As String = {Me.Session.SessionID &
".Query.", Me.Session.SessionID & ".QueryPreview"}
        'Dim sSuffixes() As String = {".DataSet", ".DataView"}

        '// EXCEPTION:  Object reference not set to an object
        '// This occurs because Me.Context is Nothing!
        '// How do I get to the Cache from here?
        oIEnumerator = Me.Context.Cache.GetEnumerator

        While (oIEnumerator.MoveNext)

           oDictionaryEntry = CType(oIEnumerator.Current,
System.Collections.DictionaryEntry)

           sKey = oDictionaryEntry.Key.ToString()

           If (sKey.StartsWith(sKeyPrefix)) Then
              Call Context.Cache.Remove(sKey)
           End If

        End While
     Catch oException As System.Exception
        '** Do nothing
        Stop
     End Try

  End Sub

How can I get to the Cache from the Session_End() event and remove
those things I need to remove?

Thanks!
Teemu Keiski - 02 Dec 2003 10:18 GMT
Hi,

ending the session might happen (and most probably does happen) outside any
web context i.e it is not tied to any particular request which again has
associated HttpContext. This means HttpContext is not necessarily available
on Session_End

You can access the cache statically via:

System.Web.HttpRuntime.Cache

Signature

Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

> [VB.NET]
>
[quoted text clipped - 7 lines]
> I know how to iterate the Cache, but the Context object is Nothing
> when the Session_End() event fires!  Take a look at my code:

'-------------------------------------------------------------------------
>    '   Session_End()

'-------------------------------------------------------------------------
>    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
>
[quoted text clipped - 35 lines]
>
> Thanks!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.