OutputCache?

A

Arpan

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
 
K

Karl Seguin [MVP]

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
 
A

Arpan

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
 
K

Karl Seguin [MVP]

Arpan:
The Cache object also isn't guaranteed not to drop. You can set a priority
(as the last argument I think)....but I even that's only a recommendation.
If this is on your own machine with little load (i.e., only you doing some
dev/test), then I wouldn't expect it to be a problem - it should cache when
you tell it to cache.

The same things that would cause your OutputCache to dump would also cause
the Cache object to dump (namely, an app reset). If you are in debug mode
(either your project or you web.config debug="true") the App Process _will_
recycle quite a bit. I'd still expect it to be pretty consistent though...so
I'm not sure this is the problem.

If you goto start --> run --> perfmon you can "Add counters" via the right
click, pick ASP.NET vXXXX from the Performance Object and "Application
Restart" shoudl be the first item..you should also seem caching counters.
You want to run this on the machine that your webserver is running.

Karl

P.S. - Option Strict in VB.NET is a nice option to turn on :)
 
A

Arpan

If this is on your own machine with little load (i.e., only you doing some
dev/test), then I wouldn't expect it to be a problem - it should cache when
you tell it to cache.

Karl, this is my own machine with 256MB RAM. I am working on Win2K Pro.
So the load shouldn't be too much but is 256MB RAM enough for a machine
which has the .NET Framework v2.0, its entire documentation & VS.NET
2005 Express Edition (along with SQL Server 2005 Management Studio
Express & Books Online) installed? The reason I am asking this is
because when I open a project in VS.NET, it takes quite a lot of time
to open the project. Closing VS.NET takes a lot of time as well (in
fact, more than the time taken to load a project). & whenever I am
working with ASP.NET & VS.NET, once everyday, Win2K generates a message
saying that my system is running on low virtual memory (or something of
that sort).

So could the 256MB RAM be the reason why the cache is behaving
erratically?

Thanks,

Regards,

Arpan
 
J

Juan T. Llibre

re:
So the load shouldn't be too much but is 256MB RAM enough for a machine
which has the .NET Framework v2.0, its entire documentation & VS.NET
2005 Express Edition (along with SQL Server 2005 Management Studio
Express & Books Online) installed?

The bare minimum for that setup is 512MB RAM; 1 GB is recommended

re:
So could the 256MB RAM be the reason why the cache is behaving erratically?

Yes.

When you run out of physical RAM, the system needs to page memory to disk.
That causes all sorts of response problems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top