Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Eric S 30 karma points
    Nov 03, 2010 @ 18:09
    Eric S
    0

    Linq to Umbraco performance issues

    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 Powell3013 karma points core c-trib
    Nov 03, 2010 @ 21:59
    Aaron Powell
    0

    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.

  • Eric S30 karma points
    Nov 03, 2010 @ 22:19
    Eric S
    0

    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.

    Thanks!

  • Aaron Powell3013 karma points core c-trib
    Nov 03, 2010 @ 23:08
    Aaron Powell
    0

    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

  • Eric S30 karma points
    Nov 05, 2010 @ 21:36
    Eric S
    0

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

  • kipusoep3707 karma points c-trib
    Nov 09, 2010 @ 12:24
    kipusoep
    0

    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

  • Jeff Grine189 karma points
    Nov 11, 2010 @ 15:06
    Jeff Grine
    0

    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?

  • kipusoep3707 karma points c-trib
    Nov 11, 2010 @ 15:17
    kipusoep
    2

    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
  • Jeff Grine189 karma points
    Nov 11, 2010 @ 15:21
    Jeff Grine
    0

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

  • Jeroen Breuer9899 karma points admin mvp c-trib
    Nov 22, 2010 @ 09:50
    Jeroen Breuer
    0

    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

  • Jeroen Breuer9899 karma points admin mvp c-trib
    Nov 22, 2010 @ 14:12
    Jeroen Breuer
    0

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

    Jeroen

     

     

  • kipusoep3707 karma points c-trib
    Nov 22, 2010 @ 17:06
    kipusoep
    1

    @Jeroen

    I'm only clearing the cache. The cache will be filled automatically again when someone requests the page (and the L2U singleton is used).
    For that, we're using a custom cache prefix (static readonly string), and use the Cache.GetEnumerator() to enumerate the keys in the cache and remove the ones which start with the custom prefix.
    I'm using HttpContent.Current.Cache, but I think it's exactly the same as HttpRuntime.Cache... (http://forums.asp.net/t/1551370.aspx)

  • Jeroen Breuer9899 karma points admin mvp c-trib
    Nov 22, 2010 @ 17:23
    Jeroen Breuer
    0

    Thanks for the info. I already did a setup myself. In the event I don't just clear the cache but also fill it by simply calling the L2U singleton again. This way the newest data is always cached when someone requests the page. So far that also seems to work :).

    Jeroen

  • Martijn van der Put65 karma points
    Jan 09, 2012 @ 16:30
    Martijn van der Put
    0

    I also used the caching method of the DataContext like Aaron Powell descibed. But I'm encountering strange behavior, I clear the cache and the next time my DataContext is called, it creates a new DataContext but it doesn't contain any new items, just the same items it contained before the update.
    I checked that the Umbraco.config file was updated before the new DataContext was created, so the issue seems to be in creating the new DataContext or some way of caching inside the DataContext.

    Did anyone encounter this issue and know a way to resolve this? (Other then forcing a AppRecycle by setting a LastWriteTime on the web.confg :( )

    I'm doing this at the end of my nightly import routine to force my website to display the most recent data. 

Please Sign in or register to post replies

Write your reply to:

Draft
Our.umbraco.org is the community mothership for Umbraco, the open source asp.net cms. With a friendly forum for all your questions, a comprehensive documentation and a ton of packages from the community.