HttpContext.Current.Cache not working.

M

msch-prv

Hello. I am trying to use caching to populate a datalist. The select
method of the associated objectdatasource calls up
GetRecipePageByRecipeCatID() to request the proper data (I am using
custom paging at SQL level
so there is a paging idx as well). The code works fine when caching
is edited out.

Problem. The cache object is only created the first time around. When
the dtl ia refreshed, th HttpContext.cache object always returns
nothing. What am I doing wrong (there is no application restart)?
Thks for your input.

Public Function GetRecipePageByRecipeCatID(ByVal RecipeCatID As
Nullable(Of Integer), _
ByVal PageIndex As Nullable(Of Integer), ByVal NumRows As
Nullable(Of Integer), ByRef RecipeCount As Integer) _
As RecipeDataSet.tblRecipesDataTable

' Set key to get/set our cache'd data
Dim strCacheKey As String = "GetRecipePageByRecipeCatID" &
RecipeCatID.ToString() & PageIndex.ToString

If HttpContext.Current.Cache(strCacheKey) IsNot Nothing
Then
Return CType(HttpContext.Current.Cache(strCacheKey),
RecipeDataSet.tblRecipesDataTable)

Else
If Not RecipeCatID.HasValue Then RecipeCatID = 0
If Not PageIndex.HasValue Then PageIndex = 0
If Not NumRows.HasValue Then NumRows = 4 ' Show 4
entries
If IsNothing(RecipeCount) Then RecipeCount = 0

Dim myDataSet As RecipeDataSet.tblRecipesDataTable =
Adapter.GetRecipePageByRecipeCatID(RecipeCatID, PageIndex, NumRows,
RecipeCount)
HttpContext.Current.Cache.Insert(strCacheKey,
myDataSet, Nothing, DateTime.Now.AddMinutes(30), TimeSpan.Zero)
Return myDataSet
End If
End Function
 
B

bruce barker

HttpContext.Current.Cache is only maintained for one request. its mainly
used for handlers to send data to each other. you want session.


-- bruce (sqlwork.com)
 
G

George Ter-Saakov

I think you are mistaken
HttpContext.Current.Cache is the same as Application.Cache and lives as
Application.
HttpContext.Current.Items is the one that lives only during that request's
lifetime....

George,.
 
M

marss

HttpContext.Current.Cache.Insert(strCacheKey,
myDataSet, Nothing, DateTime.Now.AddMinutes(30), TimeSpan.Zero)


This problem may be caused by contradiction between the absolute and
the sliding expiration.

Try this:

HttpContext.Current.Cache.Insert(strCacheKey, myDataSet, Nothing,
DateTime.Now.AddMinutes(30),
System.Web.Caching.Cache.NoSlidingExpiration)

or this:

HttpContext.Current.Cache.Insert(strCacheKey, myDataSet, Nothing,
System.Web.Caching.Cache.NoAbsoluteExpiration, new
TimeSpan(0,30,0))


Regards,
Mykola
http://marss.co.ua
 
M

msch-prv

Thanks for your replies.

I was able to solve the problem after I realized that the the system
was short on RAM (my system is limited to 770 MB RAM). So closing a
bunch of FireFox pages solved the issue. Caching works ok.

A nasty side-effect has however now creeped in: the
odsRecipeDet_Selected event of the datalist's associated
ObjectDataSource (odsRecipeDet) which is automatically triggered for
"fresh" data does not fire for cached data. This event is used to
update the custom navigation hyperlinks of the datalist. How can I re-
enable this event for cached data?

Thanks again for any hints.
 

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,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top