Setup
.NET 4.5, .NET MVC 4, IIS 8, c#
Background
I have been using HttpContext.Current.Cache
for years to cache constructed objects for a single user session in order to boost performance. This works great.
Now I need to cache constructed objects at the root level so that any user from any browser who requests a page with info that should be cached, will get that data from the cache rather than a freshly constructed object that pulled it's data from the backend.
So, I changed my caching from HttpContext.Current.Cache
to HttpRuntime.Cache
. Here is an example of adding an object to cache:
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="o">Item to be cached</param>
/// <param name="key">Name of item</param>
public static void Add<T>(T o, string key)
{
if (!string.IsNullOrEmpty(key))
{
// NOTE: Apply expiration parameters as you see fit.
HttpRuntime.Cache.Insert(
key,
o,
null,
DateTime.Now.AddDays(30),
Cache.NoSlidingExpiration);
}
}
The Problem
After implementing this, I loaded a page on my desktop in Chrome and expected it to load slowly as the first request needs to call the database and construct my objects.
Then, I went to another PC and called the same page and it took just as long! After the first request on each different PC/browser, it was lightening fast as it should be. This suggest that my objects are still being cached on a per-session basis.
A Possibility
If no one spots an obvious mistake I have made, I am wondering if possibly my issues are due to the server cluster distributing the app. So, in case anyone has insight into that, I am using Rackspace Cloud Hosting.