PDA

Click to See Complete Forum and Search --> : caching question


wsyeager36
Aug 30th, 2003, 04:40 PM
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)?

pvb
Aug 31st, 2003, 11:39 AM
I don't use the Cache's Insert and Get methods, just use Item and it works fine:
Private Function getAuthors() As DataSet
Dim ds As New DataSet
If Not Cache.Item("Authors") Is Nothing Then
Response.Write("Cache Hit!")
ds = CType(Cache.Item("Authors"), DataSet)
Else
Response.Write("Repopulating...")
Dim connString As String = "user id=sa;password=sa;database=pubs;server=DeathAngel;"
Dim cn As New SqlConnection(connString)
Dim cmdText As String = "Select * From Authors"
Dim cmd As New SqlCommand(cmdText, cn)
Dim da As New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds)
Cache.Item("Authors") = ds
End If
Return ds
End Function ... opened multiple windows after the initial hit and they all used the Cached dataset.

wsyeager36
Sep 1st, 2003, 10:47 AM
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.

pvb
Sep 1st, 2003, 11:11 AM
... 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.

wsyeager36
Sep 1st, 2003, 11:21 AM
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......

pvb
Sep 1st, 2003, 11:32 AM
hmmm, so using your code, it now looks something like this?
Dim DsCrewChief1 As DataSet
If Not Cache.Item("CrewChief") Is Nothing Then
DsCrewChief1 = CType(Cache.Item("CrewChief"), DataSet)
Else
Dim clsNCVACBLL As New NCVACBLL.Scheduling(CType(Application("strDataSource"), String))
DsCrewChief1 = New dsCrewChief
DsCrewChief1 = clsNCVACBLL.SelectCrewChief()
Cache.Item("CrewChief") = DsCrewChief1
End If and this doesn't work?

wsyeager36
Sep 1st, 2003, 11:50 AM
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.

pvb
Sep 1st, 2003, 12:21 PM
I think somethin else is going on in your code. Does the following work?CacheTest.aspx<script language="vb" runat="server">
Protected Overrides Sub OnInit(ByVal e As EventArgs)
Dim value As String
If Not Cache.Item("Test") Is Nothing Then
Response.Write("Cache Hit<br/>")
value = Cache.Item("Test")
Else
Response.Write("Repopulating<br/>")
value = "Some Value"
Cache.Item("Test") = value
End If
lblTest.Text = value
End Sub
Protected Sub btnClearCache_Click(ByVal sender As Object, ByVal e As EventArgs)
Cache.Remove("Test")
If Cache.Item("Test") Is Nothing Then
Response.Write("Cache is cleared<br/>")
Else
Response.Write("Cache was not cleared<br/>")
End If
End Sub
Protected Sub btnReload_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("CacheTest.aspx")
End Sub
</script>
<html>
<body>
<form runat="server" ID="Form1">
<asp:Label ID="lblTest" Runat="server"/><br/>
<asp:Button ID="btnClearCache" Runat="server"
Text="Clear Cache" OnClick="btnClearCache_Click"/>
<asp:Button ID="btnReload" Runat="server"
Text="Reload Page" OnClick="btnReload_Click"/>
</form>
</body>
</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?

wsyeager36
Sep 1st, 2003, 04:07 PM
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.............................

pvb
Sep 1st, 2003, 04:57 PM
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:<script language="vb" runat="server">
Protected Overrides Sub OnInit(ByVal e As EventArgs)
If Not Cache.Item("Test") Is Nothing Then
Response.Write("Cache Hit<br/>")
Response.Write(Cache.Item("Test"))
Else
Response.Write("Repopulating")
Cache.Item("Test") = "Some Value"
End If
End Sub
</script>
<html>
<body>
<form runat="server">

</form>
</body>
</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.

wsyeager36
Sep 1st, 2003, 06:56 PM
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........