Re: OutputCache?
- From: "Arpan" <arpan_de@xxxxxxxxxxx>
- Date: 31 Aug 2006 13:53:33 -0700
Karl, first of all, what are these perf counters & how & where do I add
them?
Secondly, since you have said that the OutputCache is not guaranteed to
cache for X seconds, that could be one of the reasons why the code I
have shown in post #1 behaves eccentrically but here's a sample code
wherein I am using the Cache object instead of OutputCache but still
encountering similar eccentric behavior.
Here I am adding a DataSet to the cache using the Cache object. The
DataSet holds records from a SQL Server DB table which are displayed in
a DataGrid. When this ASPX page is visited for the first time, the
DataSet gets created by explicitly retrieving the records from the DB
table but on subsequent visits (or refreshes), the DataSet with the
records is retrieved from the cache. This page also has a Button
clicking which will remove the DataSet from the cache i.e. the cache
will expire & a Label which displays from where the records are being
retrieved i.e. if the DataSet has been created using the DB table
explicitly, then the Label will display a message saying "DataSet
created from DB table" else it will display "DataSet retrieved from
cache". This is the code:
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal eas As EventArgs)
If Not (Page.IsPostBack) Then
Call CreateData()
End If
End Sub
Sub CreateData()
Dim dSet As DataSet
'retrieve the DataSet from the Cache object
dSet = Cache("DataSet")
If (dSet Is Nothing) Then
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter
sqlConn = New SqlConnection(".......")
sqlDapter = New SqlDataAdapter("SELECT * FROM tblMarks",
sqlConn)
dSet = New DataSet
sqlDapter.Fill(dSet, "Marks")
'store the DataSet in the Cache object
Cache("DataSet") = dSet
lblMessage.Text = "DataSet created from DB table"
Else
lblMessage.Text = "DataSet retrieved from cache"
End If
dgMarks.DataSource = dSet
dgMarks.DataBind()
End Sub
Sub ExpireCache(ByVal obj As Object, ByVal eas As EventArgs)
'remove the DataSet from the Cache object
Cache.Remove("DataSet")
Call CreateData()
End Sub
</script>
<form runat="server">
<asp:Label ID="lblMessage" runat="server"/><br>
<asp:Button ID="btnExpireCache" OnClick="ExpireCache" Text="EXPIRE
CACHE" runat="server"/><br>
<asp:DataGrid ID="dgMarks" runat="server"/>
</form>
Here too I find that the message (whether the DataSet has been created
from the DB table or the DataSet has been retrieved from the cache) the
Label displays are erratic. For e.g. when I visit the page for the
first time, the Label says "DataSet created from DB table"....that's
fine. Next when I refresh this page, on some occasions, the Label
wrongly displays the same message & on some other occasions, the Label
correctly displays the message "DataSet retrieved from cache".
This eccentricity also comes up when I click the "EXPIRE CACHE" Button.
When the Button is clicked, the cache expires & hence the Label should
display "DataSet created from DB table" but here too, sometimes the
Label displays this message correctly & sometimes, the Label wrongly
displays "DataSet retrieved from the cache".
Note that I am not using OutputCache anywhere in the code; so why this
eccentricity? Is it something like there is no guarantee what will be
stored in the Cache object in ASP.NET?
Thanks,
Regards,
Arpan
Karl Seguin [MVP] wrote:
Arpan:
Two thoughts.
1st - I don't think the outputcache is guaranteed to cache for X seconds.
It's more of a suggestion for ASP.NET. It'll manage it's own memory however
it thinks best.
That said, I don't think that's your problem since you are only testing it
out.
2 - The cache will dump on an application reset. If you rebuild the app,
modify the web.config, change some dlls or any other number of things, the
cache will reset. You can add perf counters to see app restarts vs cache
misses.
Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Arpan" <arpan_de@xxxxxxxxxxx> wrote in message
news:1156930728.937160.227000@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I am using the following code to cache the page output for 60 seconds:
<%@ OutputCache Duration="60" VaryByParam="*" %>
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
lblOutput.Text = "Welcome, " & Request.Params("id") & " The
time now is " & DateTime.Now.ToString("T")
End Sub
</script>
<form runat="server">
<asp:Label ID="lblOutput" runat="server"/>
</form>
Suppose I type the following URL in the address bar (assume that the
above ASPX page exists in C:\Inetpub\wwwroot\ASPX & is named
CacheOutput.aspx):
http://myserver/ASPX/CacheOutput.aspx?id=clive
Assume that the time shown is 2:43:18 PM. Ideally when I refresh the
page within a minute, the time should remain the same i.e. 2:43:18 PM.
Next I change the querystring value within a minute:
http://myserver/ASPX/CacheOutput.aspx?id=andrew
Now assume that the time shown is 2:43:40 PM. When I refresh the page
within a minute, the time should remain the same i.e. 2:43:40 PM.
Next I change the value of the querystring "id" back to "clive" within
a minute. The time shown should still be 2:43:18 PM (& not the current
time) since the page has been cached for a minute. Changing the "id" to
"andrew" again within a minute should show the time as 2:43:40 PM (&
not the current time).
But what I find is on some occasions, the page output gets cached
correctly for 60 seconds but on other occasions, the page either
doesn't get cached at all or it gets cached for a few seconds i.e. the
time shown continuously changes to the current time even when I refresh
the page within a minute.
What's causing this eccentricity?
Thanks,
Arpan
.
- Prev by Date: Re: One Subroutine after another
- Next by Date: Re: 2.0 Features
- Previous by thread: Re: One Subroutine after another
- Next by thread: Re: OutputCache?
- Index(es):
Loading