0

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Matt Cashatt
  • 23,490
  • 28
  • 78
  • 111

1 Answers1

0

Pretty sure you'll find HttpContext.Current.Cache is not user specific. That's what Session is. As stated here...

Difference between HttpRuntime.Cache and HttpContext.Current.Cache?

...there's no difference between using HttpContext.Current.Cache and HttpRuntime.Cache.

I'd suggest what is happening is that each client is caching html/images etc. The other possibility is that your application is in a server farm, in which case if your second user hits a different machine then the cache would not be populated, the cache is local to each machine.

I'd suggest you need to use some sort of performance tool to actually determine what is going on in your application. The following is a discussion on performance analysis

How to analyze the performance of requests in ASP.NET MVC application?

You could also use a tool like fiddler or charles proxy to help you determine where the time is going to on the client.

You probably also need to consider a logging tool, you should be able to easily tell from logging information whether your cache is being hit or you are loading data from the database, guessing is not something you normally do about such things. I use Log4net...

http://www.codeproject.com/Articles/14819/How-to-use-log-net

http://logging.apache.org/log4net/

It has many different means of logging e.g. email, log file, to a database table and many more.

Community
  • 1
  • 1
Mick
  • 6,527
  • 4
  • 52
  • 67