[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.caching

caching - sometimes it works and sometimes it doesn't?

Samuel

10/10/2006 6:32:00 PM


I have a weird problem using the cache in ASP.NET 2.0 on a WinXP Dual Core
Processor machine (AMD X2 3800+). I understand that each processor holds a
copy of the cache, but is it possible that one of the processor never holds a
copy?

This is my code:

================================
Public Class Forums
Public Const BaseForumCacheName As String = "The_BaseForums"

Public Shared Function GetBaseForums() As List(Of BaseForum)
Dim BaseForumCache As Object =
HttpContext.Current.Cache(BaseForumCacheName)
If BaseForumCache Is Nothing Then
Dim BaseForums As New List(Of BaseForum)
loadBaseForums(BaseForums)
Return BaseForums
Else
Return CType(BaseForumCache, List(Of BaseForum))
End If
End Function

Public Shared Sub loadBaseForums(ByVal BaseForums As List(Of
BaseForum))
Dim strSQL As String = "spGetBaseForums2"
Dim dt As New DataTable()
dt = GetDataTable(strSQL, CommandType.StoredProcedure)
Dim drs As DataRowCollection = dt.Rows
For Each dr As DataRow In drs
BaseForums.Add(New BaseForum(dr))
Next

Dim cacheTime As Double =
CDbl(ConfigurationManager.AppSettings("BaseForumCacheDurationInSeconds"))
If cacheTime > 0 Then
HttpContext.Current.Cache.Insert(BaseForumCacheName,
BaseForums, Nothing, DateTime.Now.AddSeconds(cacheTime),
Cache.NoSlidingExpiration)
End If
End Sub

End Class

===============================
BaseForum is a class I create that is the data structure, and GetDataTable
is just a helper method to return a DataTable from a query. Caching time is
currently 10 seconds. This Shared GetBaseForums() function is used as the
SelectMethod of an ObjectDataSource. The class is compiled as dll in the Bin
folder of the application.

The problem I have is that sometimes the cache mechnism doesn't seem to
work. I check using SQL profiler and found that each request generates a call
to the stored procedure "spGetBaseForums2" (called in the function
loadBaseForums above). However, sometimes it works as the SQL profiler does
not show a call to the say SP for around 10 seconds. I guess it is because I
am switch between the 2 processors, but I am not sure. How do I deal with
this issue?

Thanks for any pointers in advance!
6 Answers

offwhite

10/10/2006 9:08:00 PM

0

What you can do is handle the cache callback for when items are removed
from the cache.

http://msdn2.microsoft.com/en-us/library/system.web.caching.cacheitemremovedcal...

In your code, I do not see where you are inserting the items into the
cache. That alone would explain why it keeps hitting the stored
procedure. I suggest always getting and putting the items into the
cache in the same method so you can see it all in one place.

Once you handle the item removed callback you can log the reason it was
removed, what is being removed and when. Also log when you are adding
each item. That will help you a good deal in tracking down this
problem.

Also, I am not sure what you mean that there is a separate cache per
processor. That would only be true if you dedicated a separate
application pool per processor. There should only be one cache per
application pool. It depends on how you have your pools configured.
And you can only have a single web application living in a single pool,
so you should always have a single cache.

Brennan Stehling
http://brennan.offwhite...

Samuel wrote:
> I have a weird problem using the cache in ASP.NET 2.0 on a WinXP Dual Core
> Processor machine (AMD X2 3800+). I understand that each processor holds a
> copy of the cache, but is it possible that one of the processor never holds a
> copy?
>
> This is my code:
>
> ================================
> Public Class Forums
> Public Const BaseForumCacheName As String = "The_BaseForums"
>
> Public Shared Function GetBaseForums() As List(Of BaseForum)
> Dim BaseForumCache As Object =
> HttpContext.Current.Cache(BaseForumCacheName)
> If BaseForumCache Is Nothing Then
> Dim BaseForums As New List(Of BaseForum)
> loadBaseForums(BaseForums)
> Return BaseForums
> Else
> Return CType(BaseForumCache, List(Of BaseForum))
> End If
> End Function
>
> Public Shared Sub loadBaseForums(ByVal BaseForums As List(Of
> BaseForum))
> Dim strSQL As String = "spGetBaseForums2"
> Dim dt As New DataTable()
> dt = GetDataTable(strSQL, CommandType.StoredProcedure)
> Dim drs As DataRowCollection = dt.Rows
> For Each dr As DataRow In drs
> BaseForums.Add(New BaseForum(dr))
> Next
>
> Dim cacheTime As Double =
> CDbl(ConfigurationManager.AppSettings("BaseForumCacheDurationInSeconds"))
> If cacheTime > 0 Then
> HttpContext.Current.Cache.Insert(BaseForumCacheName,
> BaseForums, Nothing, DateTime.Now.AddSeconds(cacheTime),
> Cache.NoSlidingExpiration)
> End If
> End Sub
>
> End Class
>
> ===============================
> BaseForum is a class I create that is the data structure, and GetDataTable
> is just a helper method to return a DataTable from a query. Caching time is
> currently 10 seconds. This Shared GetBaseForums() function is used as the
> SelectMethod of an ObjectDataSource. The class is compiled as dll in the Bin
> folder of the application.
>
> The problem I have is that sometimes the cache mechnism doesn't seem to
> work. I check using SQL profiler and found that each request generates a call
> to the stored procedure "spGetBaseForums2" (called in the function
> loadBaseForums above). However, sometimes it works as the SQL profiler does
> not show a call to the say SP for around 10 seconds. I guess it is because I
> am switch between the 2 processors, but I am not sure. How do I deal with
> this issue?
>
> Thanks for any pointers in advance!

Samuel

10/11/2006 12:40:00 AM

0

Thanks for your reply. I did add item to the cache in the loadBaseForums
function. And as I said, sometimes caching does work, but sometimes it
doesn't. I will try your suggestion to see item is indeed added and why it
was removed.

"offwhite" wrote:

> What you can do is handle the cache callback for when items are removed
> from the cache.
>
> http://msdn2.microsoft.com/en-us/library/system.web.caching.cacheitemremovedcal...
>
> In your code, I do not see where you are inserting the items into the
> cache. That alone would explain why it keeps hitting the stored
> procedure. I suggest always getting and putting the items into the
> cache in the same method so you can see it all in one place.
>
> Once you handle the item removed callback you can log the reason it was
> removed, what is being removed and when. Also log when you are adding
> each item. That will help you a good deal in tracking down this
> problem.
>
> Also, I am not sure what you mean that there is a separate cache per
> processor. That would only be true if you dedicated a separate
> application pool per processor. There should only be one cache per
> application pool. It depends on how you have your pools configured.
> And you can only have a single web application living in a single pool,
> so you should always have a single cache.
>
> Brennan Stehling
> http://brennan.offwhite...
>
> Samuel wrote:
> > I have a weird problem using the cache in ASP.NET 2.0 on a WinXP Dual Core
> > Processor machine (AMD X2 3800+). I understand that each processor holds a
> > copy of the cache, but is it possible that one of the processor never holds a
> > copy?
> >
> > This is my code:
> >
> > ================================
> > Public Class Forums
> > Public Const BaseForumCacheName As String = "The_BaseForums"
> >
> > Public Shared Function GetBaseForums() As List(Of BaseForum)
> > Dim BaseForumCache As Object =
> > HttpContext.Current.Cache(BaseForumCacheName)
> > If BaseForumCache Is Nothing Then
> > Dim BaseForums As New List(Of BaseForum)
> > loadBaseForums(BaseForums)
> > Return BaseForums
> > Else
> > Return CType(BaseForumCache, List(Of BaseForum))
> > End If
> > End Function
> >
> > Public Shared Sub loadBaseForums(ByVal BaseForums As List(Of
> > BaseForum))
> > Dim strSQL As String = "spGetBaseForums2"
> > Dim dt As New DataTable()
> > dt = GetDataTable(strSQL, CommandType.StoredProcedure)
> > Dim drs As DataRowCollection = dt.Rows
> > For Each dr As DataRow In drs
> > BaseForums.Add(New BaseForum(dr))
> > Next
> >
> > Dim cacheTime As Double =
> > CDbl(ConfigurationManager.AppSettings("BaseForumCacheDurationInSeconds"))
> > If cacheTime > 0 Then
> > HttpContext.Current.Cache.Insert(BaseForumCacheName,
> > BaseForums, Nothing, DateTime.Now.AddSeconds(cacheTime),
> > Cache.NoSlidingExpiration)
> > End If
> > End Sub
> >
> > End Class
> >
> > ===============================
> > BaseForum is a class I create that is the data structure, and GetDataTable
> > is just a helper method to return a DataTable from a query. Caching time is
> > currently 10 seconds. This Shared GetBaseForums() function is used as the
> > SelectMethod of an ObjectDataSource. The class is compiled as dll in the Bin
> > folder of the application.
> >
> > The problem I have is that sometimes the cache mechnism doesn't seem to
> > work. I check using SQL profiler and found that each request generates a call
> > to the stored procedure "spGetBaseForums2" (called in the function
> > loadBaseForums above). However, sometimes it works as the SQL profiler does
> > not show a call to the say SP for around 10 seconds. I guess it is because I
> > am switch between the 2 processors, but I am not sure. How do I deal with
> > this issue?
> >
> > Thanks for any pointers in advance!
>
>

Samuel

10/11/2006 1:51:00 AM

0

I recorded the time cache is created and the reason cache is removed on a
file system, and I found something very weird. The following is a small
section of the log:

2006-10-10T18:40:25 - Cache Applications_HashTable inserted successfully.
2006-10-10T18:40:28 - Cache Applications_HashTable was removed reason:
Underused

I have seen this twice in the log where the Applications_HashTable (the key
of the cache) is removed within "seconds" it is inserted. I looked up the
text and saw:

"Underused - The item is removed from the cache because the system removed
it to free memory. "

However, my computer is not short of memory at all - I have got tons of
memory on my dev computer and I know I am running a lot of things at the same
time, but the memory I have left is still alot. Can someone help me find out
the reason this keeps happening and how I can stop this.

Thanks!

vecozo@online.nospam

10/13/2006 8:24:00 AM

0


Hi Samuel,

I would suggest you write a (unit)test to gain a deeper understanding of the
caching behaviour.


Pseudocode:

[testMethod]
public void Test() {

while (true)
GetBaseForums()
thread.sleep(10); // wait 10 m,illisedons
}

}

Now examine your logs and you'll probably find that your cache is updated
every ten seconds.

Regards,
Martijn Kaag


"Samuel" wrote:

> I recorded the time cache is created and the reason cache is removed on a
> file system, and I found something very weird. The following is a small
> section of the log:
>
> 2006-10-10T18:40:25 - Cache Applications_HashTable inserted successfully.
> 2006-10-10T18:40:28 - Cache Applications_HashTable was removed reason:
> Underused
>
> I have seen this twice in the log where the Applications_HashTable (the key
> of the cache) is removed within "seconds" it is inserted. I looked up the
> text and saw:
>
> "Underused - The item is removed from the cache because the system removed
> it to free memory. "
>
> However, my computer is not short of memory at all - I have got tons of
> memory on my dev computer and I know I am running a lot of things at the same
> time, but the memory I have left is still alot. Can someone help me find out
> the reason this keeps happening and how I can stop this.
>
> Thanks!

lukezhan

10/13/2006 8:43:00 AM

0

Hello,

The ASP.NET may not consume all memory of a server, have you found how many
memory the w3wp.exe process consume when the object was removed from the
cache? Also, in IIS Application pool's properties setting, we also can set
how many memory an application can use.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/de....
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.



Samuel

10/17/2006 10:49:00 PM

0

very little.. 70MB or something like that. I am using WinXP as the dev
platform so I can't adjust the memory usage. I tried using notremovable as
the priority to stop this weird behavior and it worked. I saw from another
post online a Microsoft mvp mentioned that the cache cleaning behavior in
ASP.NET 2.0 was overly aggressive.

"Luke Zhang [MSFT]" wrote:

> Hello,
>
> The ASP.NET may not consume all memory of a server, have you found how many
> memory the w3wp.exe process consume when the object was removed from the
> cache? Also, in IIS Application pool's properties setting, we also can set
> how many memory an application can use.
>
> Sincerely,
>
> Luke Zhang
>
> Microsoft Online Community Support
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
> ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/de....
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
>