0

I've got this piece of caching code

public static ICollection<Messages> GetMessages()
{
    if (System.Web.HttpContext.Current.Cache["GetMessages_" + user_id] == null)
    {
        using (DataContext db = new DataContext())
        {
            var msgs = (from m in db.Messages 
                        where m.user_id == user_id 
                        && m.date_deleted == null                
                        select m).ToList();

            System.Web.HttpContext.Current.Cache.Insert(
                "GetMessages_" + user_id, msgs, null, 
                 DateTime.Now.AddMinutes(5), TimeSpan.Zero);
        }
    }
    return System.Web.HttpContext.Current.Cache["GetMessages_" + user_id] 
               as ICollection<Messages>;
}

The first time it runs, it pulls the data from a SQL table, and takes about 1500 ms. Every subsequent call takes about 600ms. The collection i'm testing on currently contains just 3 objects, each with minimal data (a string, 3 datetime fields, 3 bools and 5 ints)

Is this normal? loading a page with this tiny amount of data on it takes almost 2 seconds, every single time.

[FYI this is just running on a dev machine, not a fully fledged web server. data is being pulled from a remote server but that should only affect the initial page load]

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
roryok
  • 9,325
  • 17
  • 71
  • 138
  • Are you by chance noting this slowness while running it in the VS development web server? – moribvndvs Mar 12 '12 at 15:43
  • yes... is that likely to be the issue? – roryok Mar 12 '12 at 16:01
  • 2
    Of course, debugging is going to be slower, and the development web server is not going to have the performance of IIS, however, I've seen lots of complaints about how slow it is, and experienced it myself. There are a few things you could look into: http://stackoverflow.com/questions/5237340/very-slow-loading-time-with-visual-studio-and-asp-net-mvc and http://stackoverflow.com/questions/4264100/why-is-the-asp-net-visual-studio-web-development-server-so-slow – moribvndvs Mar 12 '12 at 16:05
  • @HackedByChinese, good stuff! Would you mind turning this into an answer? Hope roryok will accept. – Gert Arnold Mar 12 '12 at 20:56
  • yeah, I'll accept that if you post it. – roryok Mar 14 '12 at 10:20

2 Answers2

0

As noted by HackedByChinese, this was running in the VS development web server which of course slowed things down. However, I did find that my programming style was lending itself to very slow load times.

I used the method above to load a cached collection of messages, and also similar methods to pull back other entities from the database. One such item was a 'Settings' object.

On the settings page, I would call the function each time I wanted to return a property. For instance Cache.GetSettings().username, then Cache.GetSettings().useremail etc. I assumed that this would make the app run a little faster, as it's retrieving the object from the cache each time, without needing to access the database. But obviously, cache is not as fast as memory. Each call was taking about half a second (on the local server) and maybe .1s or .2s on the remote server. I realised if I set this to a variable...

var settings = Cache.GetSettings();

and referenced that instead, load times dropped significantly. There was a single 0.5s load at this line, and then every subsequent reference to settings took no time at all.

So to sum up, yes, cache can be slow, but only if you use it in a stupid manner like I did!

roryok
  • 9,325
  • 17
  • 71
  • 138
0

Please remember that when you create first Context in EF code first approach - EF has to rebuild model from metadata which slow down first invokation.

Vitaliy Kalinin
  • 1,791
  • 12
  • 20