Results 1 to 11 of 11

Thread: caching question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17

    caching question

    I thought that if I set up caching, it is supposed to stay resident in the web server unless I restart it.

    IN my following code-behind, the very first time in the page for every user, it always populates the cache. I am expecting the cache to be populated only once until I remove it.

    <code>
    DsCrewChief1 = Cache.Get("CrewChief")
    If DsCrewChief1 Is Nothing Then 'it goes into this if statement the first time thru the page
    Dim clsNCVACBLL As New NCVACBLL.Scheduling(CType(Application("strDataSource"), String))
    DsCrewChief1 = New dsCrewChief
    DsCrewChief1 = clsNCVACBLL.SelectCrewChief()
    Cache.Insert("CrewChief", DsCrewChief1, Nothing)
    clsNCVACBLL.Dispose()
    End If
    </code>

    This is at the top of my HTML:
    <code>
    <%@ Reference Page="..\Default.aspx" %>
    <%@ Register TagPrefix="mbrsc" Namespace="MetaBuilders.WebControls" Assembly="MetaBuilders.WebControls.RowSelectorColumn" %>
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="SignUp.aspx.vb" Inherits="NCVAC.SignUp"%>
    </code>

    On subsequent postbacks, it does read the data from cache. But, if I end my browser session and start the project again, it re-populates the cache which, again, I'm not expecting it to do. I'm expecting it to read it from cache since the dataset should be populated. I am NOT setting this dataset to NOTHING anywhere in the code...

    What concept am I missing here? Am I supposed to set something up on the webserver (IIS5.1)?
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    I don't use the Cache's Insert and Get methods, just use Item and it works fine:
    VB Code:
    1. Private Function getAuthors() As DataSet
    2.     Dim ds As New DataSet
    3.     If Not Cache.Item("Authors") Is Nothing Then
    4.         Response.Write("Cache Hit!")
    5.         ds = CType(Cache.Item("Authors"), DataSet)
    6.     Else
    7.         Response.Write("Repopulating...")
    8.         Dim connString As String = "user id=sa;password=sa;database=pubs;server=DeathAngel;"
    9.         Dim cn As New SqlConnection(connString)
    10.         Dim cmdText As String = "Select * From Authors"
    11.         Dim cmd As New SqlCommand(cmdText, cn)
    12.         Dim da As New SqlDataAdapter(cmd)
    13.         ds = New DataSet
    14.         da.Fill(ds)
    15.         Cache.Item("Authors") = ds
    16.     End If
    17.     Return ds
    18. End Function
    ... opened multiple windows after the initial hit and they all used the Cached dataset.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17

    caching question

    Yes, but did it use the data from the cached dataset after you closed your browser session?

    Mine works fine if I'm using the same browser session.
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    ... opened multiple windows after the initial hit and they all used the Cached dataset.
    ...yes, open and close the browsers all ya want, after the first hit it will use the cached dataset. And I just checked the test project I built yesterday by opening a new browser window and it's still hitting cache.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17

    Red face caching question.

    For whatever reason, my cache is not working in between browser sessions........

    After replacing

    "DsCrewChief1 = Cache.Get("CrewChief")
    If DsCrewChief1 Is Nothing Then"

    with "If Cache.Item("CrewChief") Is Nothing Then", there is still nothing in the cache in between browser sessions......
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    hmmm, so using your code, it now looks something like this?
    VB Code:
    1. Dim DsCrewChief1 As DataSet
    2. If Not Cache.Item("CrewChief") Is Nothing Then
    3.     DsCrewChief1 = CType(Cache.Item("CrewChief"), DataSet)
    4. Else
    5.     Dim clsNCVACBLL As New NCVACBLL.Scheduling(CType(Application("strDataSource"), String))
    6.     DsCrewChief1 = New dsCrewChief
    7.     DsCrewChief1 = clsNCVACBLL.SelectCrewChief()
    8.     Cache.Item("CrewChief") = DsCrewChief1
    9. End If
    and this doesn't work?

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17

    Red face caching question

    Correct.....

    It looks exactly like that............

    Again, it works fine within the same broswer session. However, after I end my browser session and start up the website again, there is nothing in the cache.
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

  8. #8
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    I think somethin else is going on in your code. Does the following work?CacheTest.aspx
    VB Code:
    1. <script language="vb" runat="server">
    2. Protected Overrides Sub OnInit(ByVal e As EventArgs)
    3.     Dim value As String
    4.     If Not Cache.Item("Test") Is Nothing Then
    5.         Response.Write("Cache Hit<br/>")
    6.         value = Cache.Item("Test")
    7.     Else
    8.         Response.Write("Repopulating<br/>")
    9.         value = "Some Value"
    10.         Cache.Item("Test") = value
    11.     End If
    12.     lblTest.Text = value
    13. End Sub
    14. Protected Sub btnClearCache_Click(ByVal sender As Object, ByVal e As EventArgs)
    15.     Cache.Remove("Test")
    16.     If Cache.Item("Test") Is Nothing Then
    17.         Response.Write("Cache is cleared<br/>")
    18.     Else
    19.         Response.Write("Cache was not cleared<br/>")
    20.     End If
    21. End Sub
    22. Protected Sub btnReload_Click(ByVal sender As Object, ByVal e As EventArgs)
    23.     Response.Redirect("CacheTest.aspx")
    24. End Sub
    25. </script>
    26. <html>
    27.     <body>
    28.         <form runat="server" ID="Form1">
    29.             <asp:Label ID="lblTest" Runat="server"/><br/>
    30.             <asp:Button ID="btnClearCache" Runat="server"
    31.                 Text="Clear Cache" OnClick="btnClearCache_Click"/>
    32.             <asp:Button ID="btnReload" Runat="server"
    33.                 Text="Reload Page" OnClick="btnReload_Click"/>
    34.         </form>
    35.     </body>
    36. </html>
    ..here's a working example http://www.panteravb.com/samples/CacheTest.aspx
    after I end my browser session and start up the website again,
    ...i assume by start up the website again you just mean opening a new browser window and not actually restarting the website, right?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17

    Red face

    To the latter, yes that's correct. When I start up a new browser session, there is nothing in the cache.

    I executed the code you sent. The first time thru, it populates the cache which is what I am expecting. The second time thru ( during the same browser session - pressing the "Reload" button ) it grabs the data from the cache and produces the attached error:

    After ending the browser session, I start up another one and again, there is nothing in the cache. It needs to repopulate the cache.............................
    Attached Files Attached Files
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

  10. #10
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    That error has nothing to do with cache, it just can't find the page you have set up, it needs the correct path to that page. My bad, I probably made it confusing by adding too much to the example. This is the simplest example I can think of, I can't make the Cache not work, so if this doesn't work I give up:
    VB Code:
    1. <script language="vb" runat="server">
    2. Protected Overrides Sub OnInit(ByVal e As EventArgs)
    3.     If Not Cache.Item("Test") Is Nothing Then
    4.         Response.Write("Cache Hit<br/>")
    5.         Response.Write(Cache.Item("Test"))
    6.     Else
    7.         Response.Write("Repopulating")
    8.         Cache.Item("Test") = "Some Value"
    9.     End If 
    10. End Sub
    11. </script>
    12. <html>
    13.     <body>
    14.         <form runat="server">
    15.            
    16.         </form>
    17.     </body>
    18. </html>
    So to claify what should happen. You surf to the this example at say /localhost/cachetest.aspx. If it's the first time to this page, you'll see the words Repopulating on the screen. Hit your F5 key on your browser and you should see Cache Hit followed by the words Some Value.

    Now close the browser. Start up a new browser window and type in /localhost/cachetest.aspx(or whereever that file is) and you will now see Cache Hit and Some Value.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2003
    Location
    Weston, FL USA
    Posts
    17

    Talking

    THANK YOU!!!

    Here's what the problem was. There was nothing wrong with my code. What I was doing was starting a new browser session from within my IDE. In there, the first time thru (for each new browser session), the cache is empty. If I launch another browser session without going into my IDE and going to the web page, it grabs the data from cache.

    I thought that even from within the IDE, it schould act like it does when not inside the IDE.

    Thanks a lot........
    Thanks,

    Bill Yeager (BCIP, MCP)
    BrainBench Certified Internet Professional
    Microsoft Certified Professional
    YeagerTech Consulting, Inc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width