2

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?

John Cruz
  • 1,562
  • 4
  • 14
  • 32

2 Answers2

2

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.

Community
  • 1
  • 1
Ryan O'Hara
  • 311
  • 1
  • 10
  • Actually, only one Cache. – John Saunders Dec 18 '14 at 21:54
  • @Ryan Thanks for the pointer. The only odd thing I have is iOS and Firefox browsers are showing the same timestamp now, but Chrome shows a different one. Would you happen to know why Chrome is different? – John Cruz Dec 19 '14 at 14:51
  • @John I am not sure why chrome would be acting different than other browsers. Are you running your application on a server farm? – Ryan O'Hara Dec 19 '14 at 15:14
  • @Ryan Yes. It is in Rackspace CloudSites. I think I know what you're referring to with that question. I guess the service is probably virtually loaded on many scaling machines, and there is a separate cache on each. Interesting. – John Cruz Dec 20 '14 at 14:40
0

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Sean
  • 46
  • 3