httpcontext.current OR httpruntime ?

G

Guest

in asp.net 2,
I load a dataset from the Cache object into a local object to use to perform
processing. I had been using httpcontext.current.cache and what I noticed is
sometime I'm not able to get the dataset out of the cache.
the application is under heavy hits, and is set to email me on every error.
and once or twice every month I get these error e-mail about this issue.

why does the HTTPContext.Current.Cache call work sometimes, while some other
times does not.
since I changed it to httpruntime, I did not get any error (until now) and
I was able to always get my dataset back out of the cache.

am I doing it correctly by using httpruntime ?
 
G

Guest

the HttpContext.Cache is just an alias to the HttpRuntime.Cache, so it makes
no difference which you use.

The cache is only valid during the life of the request. how is you code
using the cache?


-- bruce (sqlwork.com)
 
G

Guest

Zino,
ASP.NET applications recycle periodically, especially if there is a lot of
memory pressure. When this happens, Cache goes away and is then resurrected.
While there could be other explanations, this is the most likely one.

It should not matter whether you use HttpContext or HttpRuntime, as Bruce
indicated they both point at the same animal.
-- Peter
// It works on my machine!
http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
 
G

George Ter-Saakov

Cache is valid during lifetime of application not request.....

George.


"bruce barker (sqlwork.com)"
 
G

Guest

recycling doesn't effect HttpRuntime.Cache, which only exists for the
request. during a recycle, currently running requests continue in the old
appdomain, so they still have this data.

-- bruce (sqlwork.com)
 
G

Guest

here how I'm using the cache:

Public Class CacheFactory
Delegate Function myDelegate() As DataSet

Public Overloads Shared Function GetCachedItem(ByVal itemKey As CacheEnum,
ByVal funct As myDelegate) As System.Web.Caching.Cache
If HttpRuntime.Cache(itemKey) Is Nothing Then
Dim ds As DataSet = funct.Invoke()
HttpRuntime.Cache.Insert(itemKey, ds, Nothing)
End If
Return HttpRuntime.Cache
End Function

End Class



then I have a function in another class that use the cache

Public Function GetAllClients(ByVal serviceCenter As String) As DataRowView()
Dim myDelegate As New CacheFactory.myDelegate(AddressOf
ClientDAL.GetAllClients) 'call ClientDAL.GetAllClients
Dim myCache As System.Web.Caching.Cache =
CacheFactory.GetCachedItem(CacheEnum.clientCode, myDelegate) ' that's where
the error is generated
Dim ds As DataSet = CType(myCache(myCacheEnum.clientCode), DataSet)
.... ... .
... .. .
End Function
 
G

Guest

That doesn't sound accurate.

The System.Web.Caching.Cache class can be accessed either through the static
property System.Web.HttpRuntime.Cache or through the helper instance
properties System.Web.UI.Page and System.Web.HttpContext.Cache. It is
therefore available outside the context of a request. There is only one
instance of this object throughout an entire application domain.

Recycling an AppDomain (e.g. by touching web.config, or due to IIS causing
the recycle due to memory pressure, etc.) will drop Application, Cache, and
in-process Session information since that is stored in the AppDomain logical
process.

You can do this simple test:

protected void Page_Load(object sender, EventArgs e)
{
Cache["test"] = "Stuff in Cache";
System.Web.HttpRuntime.UnloadAppDomain();
Response.Redirect("Default2.aspx");
// Default2.aspx simply does: Response.Write((string)
Cache["test"]); -- you should see nothing.
}
-- Peter
// It works on my machine!
http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
 

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

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top