Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jan 24, 2011 @ 12:03
    Ismail Mayat
    0

    Singleton caching linq2umbraco context

    Guys,

    Got a weird problem.  I am using linq2umbraco with umbraco 452 (.net 40) on a page and the context is rebuilt each time that page is hit e.g

    WoiDataContext _woiContext = new WoiDataContext();

    I recall from some tweets a couple of weeks ago from Slace that you should use singleton pattern for your linq2umbraco context.  

    So I updated my code and am using singleton and caching my linq2umbraco context. I also have action handlers for publish / unpublish which removes the linq2umbraco context from cache.  Singleton code looks like

    public partial class WoiDataContext
        {
            const int ValueCacheTime = 86400;
            public const string ContextCacheKey = "WOIContext";
    
            public static WoiDataContext Instance
            {
                get
                {        
                    if (UmbracoContext.Current.InPreviewMode)
                    {
                        var previewContent = new PreviewContent(new Guid(umbraco.BusinessLogic.StateHelper.GetCookieValue("PreviewSet")));
                        var path = previewContent.PreviewsetPath;
                        return new WoiDataContext(new NodeDataProvider(path, true));
                    }
                    //using http://aspalliance.com/1705_A_New_Approach_to_HttpRuntimeCache_Management.all to cache
                    return TCache<WoiDataContext>.Get(ContextCacheKey, ValueCacheTime,() => new WoiDataContext());
    
                }
            }
    
            public static void RemoveLinq2UmbracoContextFromCache()
            {
                TCache.Update(ContextCacheKey);
            }
        }

    when i run the website locally (iis7.5) the item is added to cache i can see it in umbraco cache browser.  When i run the site on dev server (iis6) it does not cache. I dont get any errors and site still runs.  This is really really weird.

    I checked the dlls on the dev server and my caching code is definately deployed.  For some strange reason its just not caching.

    Any ideas?

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jan 24, 2011 @ 12:24
    Ismail Mayat
    0

    This seems to be iis6 issue. I deployed my dll to live iis7.5 site and it works the context is cached.  Bizarre!!!

    Regards

    Ismail

  • Harv 18 posts 38 karma points
    Apr 12, 2011 @ 18:15
    Harv
    0

    Hi Ismail,

    I was very interested to ready this post as I'm trying to implement something very similar, except I'm using the multithreaded singleton pattern.

    Anyway the problem I'm getting is when I create a new instance of my DataContext it's created in a nanosecond, but it seems there's a process in the background that actually populates the object graph, as there is no data in the object at this point.

    If I run a linq query when this is going on, all I have is an empty instance of the DataContext with no data? The net result is that each time I refresh the data, users of the website get errors until the DataContext gets all the data, which can take up to 20 secs on my site.

    I've also tried the NodeDataProvider "Flush" method but this is unreliable, it only seems to work 60% of the time.

    I'd be grateful for any thoughts.

    Thanks

    Harv

  • Thomas Dolberg 74 posts 95 karma points
    Apr 13, 2012 @ 10:04
    Thomas Dolberg
    0

    Hi Harv

     

    did you resolve the problem ? I am having similar issues with long loadtimes (in my case up to 2 minutes because I have a lot of nodes).

     

    thanks

    Thomas

  • Harv 18 posts 38 karma points
    Apr 13, 2012 @ 11:05
    Harv
    0

    Hi Thomas,

    I did solve my problem - by ripping out the LinqToUmbraco and using Sql Server instead :-D

    Umbraco is a very good CMS but it's not designed to handle lots of data, so if you're having long loading times maybe you have too much data for Umbraco to handle? In these cases we use Sql Server with a custom section in the Umbraco admin system to manage the data.

    Here's how to make the custom sections:
    http://www.geckonewmedia.com/blog/2009/8/3/how-to-create-a-custom-section-in-umbraco-4

    Cheers

    H

  • Thomas Dolberg 74 posts 95 karma points
    Apr 13, 2012 @ 12:01
    Thomas Dolberg
    0

    Hi Harv

     

    thank you for your reply. We rely heavily on LinqToUmbraco to generate strongly-typed objects as well as for the data retrieval itself. And I would really like to continue having the strongly-typed objects.

    We are creating a site where users can update their profile. But because of the large amount of data, whenever a profile is changed by the user, the entire datacontext has to be reloaded, which can take up to 2 minutes, which is quite bad for the user experience. 

    Are there any other ways of creating strongly-typed objects in umbraco or make LinqToUmbraco faster with a lot of data ?

     

    thanks

    Thomas

  • Harv 18 posts 38 karma points
    Apr 13, 2012 @ 12:59
    Harv
    0

    IMO LinqToUmbraco is unuseable for that very reason, but it may have been updated since I last used it. I use razor templates now where you get strongly typed objects by default.

    For example if you have a Profile document type with 3 properties; FirstName, LastName, Email; on your page you accesss that data using with the following c# code; @Model.FirstName, @Model.LastName, @Model.Email. Likewise you can access any document or multiple documents from any page in your site.

    Here are some tutorials on razor:
    http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets

    Thanks

    H

  • Thomas Dolberg 74 posts 95 karma points
    Apr 13, 2012 @ 13:07
    Thomas Dolberg
    0

    Hi H,

    I also use my strongly typed objects in the business layer. As far as I know that is not possible with Razor, since it is a rendering engine, or am I mistaken.

     

    thanks

    Thomas

  • Harv 18 posts 38 karma points
    Apr 13, 2012 @ 15:10
    Harv
    0

    Yes you're correct about razor.

    It seems to me you should be using Sql Server for this part of your project, Umbraco is not designed to store lots of data as it's just a CMS. 

     

  • Thomas Dolberg 74 posts 95 karma points
    Apr 14, 2012 @ 01:01
    Thomas Dolberg
    0

    You are probably right. Since I already have all my "objects" setup as doctypes in umbraco,  is there an easy way to create strongly-typed objects using sql and the umbraco db ? Or should I avoid umbraco all together and use custom tables ?

     

    thanks

    Thomas

Please Sign in or register to post replies

Write your reply to:

Draft