x First time here? Check out the FAQ
Please report confirmed 4.1 bugs in our issue tracker at CodePlex
  • Avatar10posts30karma

    Linq to Umbraco performance issues

    Eric S started this topic November 3, 2010 @ 06:09, Go directly to the topic solution

    Hi there,

    We have a site that we built in Umbraco 4.5.2 that has over 1000 nodes, and we discovered that any time we were using Linq to Umbraco, it would easily add a second to the load time and quickly max out the CPU on the server with multiple people hitting the site. We had a bit of code that was using Linq to Umbraco on every page load (not smart, I know...), and it was causing the site to be extremely slow and peg the CPU at 100% during "high" traffic times (1000 hits an hour?).

    Needless to say, we've looking at other ways of doing things, but has anyone else ran into this problem?


  • Aaron Powell posted this reply November 3, 2010 @ 09:59

    Are you disposing of the DataContext when you're done or are you keeping it active for the life of the app pool?

    You really should be keeping it alive as a singleton so that the cache is used within LINQ to Umbraco.

  • Avatar10posts30karma
    Comment with ID: 52440
    Eric S posted this reply November 3, 2010 @ 10:19

    Right now we're using a new one every time. Definitely is not taking advantage of any caching then... I'll give it a try.

    I've never used a singleton in a web app before, but I assume it works like any other, just needs to be thread safe. Looks like http://msdn.microsoft.com/en-us/library/ms998558 has a good example of one, i'll see if that does the trick.


  • Avatar1690posts3009karma
    CoreComment with ID: 52441
    Aaron Powell posted this reply November 3, 2010 @ 11:08

    I don't make my own singletons, I use a dependency injection framework for that. My one of choice is Autofac - http://code.google.com/p/autofac

  • Avatar10posts30karma
    Comment with ID: 52755
    Eric S posted this reply November 5, 2010 @ 09:36

    Tried out the singleton for the DataContext, and it's improved the site performance in a lot of areas of the site. Thanks again!

  • Avatar1448posts3536karma
    CoreComment with ID: 52960
    kipusoep posted this reply November 9, 2010 @ 12:24

    Oh man, this thread helped me a lot!

    I was using Singleton with HttpContext.Current.Items, which means no cache.
    Now using HttpContent.Current.Cache and it's very fast! :D

  • Avatar149posts189karma
    Comment with ID: 53306
    Jeff Grine posted this reply November 11, 2010 @ 03:06

    I was doing the same thing. I think I got that from Aaron's video? But if we use cache instead of items, then don't we run the risk of having stale data? Would that mean Linq to Umbraco isn't appropriate for large sites where you need it to have the most current data?

  • Avatar1448posts3536karma
    CoreComment with ID: 53309
    kipusoep posted this reply November 11, 2010 @ 03:17

    Well, I hooked into the Document's event model to clear the Linq2Umbraco cache item when updates are done, for these events:

    • Document.New
    • Document.AfterPublish
    • Document.AfterUnPublish
    • Document.AfterDelete
    • Document.AfterMoveToTrash

  • Avatar149posts189karma
    Comment with ID: 53310
    Jeff Grine posted this reply November 11, 2010 @ 03:21

    Perfect, thanks. A little extra wiring to remember, but that should make a big difference in performance.

  • Jeroen Breuer posted this reply November 22, 2010 @ 09:50

    Hi Kipusoep,

    Do you just clear the cache or do you also fill the cache with the new Linq2Umbraco data? Do you have a sample?


  • Jeroen Breuer posted this reply November 22, 2010 @ 02:12

    P.s. do you use HttpRuntime.Cache or HttpContext.Current.Cache?





Please login or Sign up To post replies