System.Web.HttpContext.Current.Cache Different Results Returned In Different Browsers

2.3k Views Asked by At

I have a working ASP.Net web service that caches results using System.Web.HttpContext.Current.Cache (Insert and Get). For testing purposes, I show the time in the web service results.

On the same browser, it properly caches and doesn't refresh until the 1 minute expiration that I set.

If I run the same web service in another browser (even on same machine) it returns a DIFFERENT time and then that is cached properly per minute. The previous browser still shows its old results (until time expires).

Testing on iPhone with Safari does the same thing (different cached results than two other browsers).

Why are the cached results DIFFERENT PER BROWSER? I'm a bit new to caching, so I clearly am missing something here. I'm trying to cache results for EVERYONE, not just the same person on the same browser. I would expect the time returned to be the SAME for all users in any browser.

This is the code I run:

 HttpContext.Current.Cache.Insert("GetIDList", sJSON, Nothing, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration)

What am I missing?

2

There are 2 best solutions below

4
On BEST ANSWER

You are setting the HttpContext object for the current request. This is why each browser is having its own cache set and you are seeing different times for each user. You can set this to httpContext.cache and set the cache to current application domain. MSDN

This uses HttpRuntime.cache anyways to do the caching so use HttpRuntime.cache anyways.

Set the Cache for the current application using httpRuntime.cache MSDN

HttpRuntime.Cache.Insert("GetIDList", sJSON, Nothing, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration)

There is another Post here that helps explain httpContext.Cache Vs HttpRuntime.Cache a little better.

I hope this helps.

1
On

Cache is stored on the client's browser, so basically you are simply telling the server to store the cache object in the user's browser when the insert method is called. MSDN has some solid documentation on utilizing cache: http://msdn.microsoft.com/en-us/library/xsbfdd8c.aspx