Copied to clipboard

Flag this post as spam?

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


  • Scott Beveridge 4 posts 24 karma points
    Dec 01, 2011 @ 18:44
    Scott Beveridge
    0

    Linq to Umbraco singleton datacontext and document updates

    Hi

    I have a linq to umbraco singleton datacontext which really speeds things up for my site which has over 1000 nodes.

    I want to know how to refresh the singleton so that when I create or modify documents using the Document API, these changes are pushed into the linq to umbraco datacontext....

    Thank

  • Henri Toivonen 77 posts 111 karma points
    Dec 02, 2011 @ 10:48
    Henri Toivonen
    0

    I'm not sure, and haven't tried, but shouldn't you be able to just "foo = null; foo = new DataContext();"? Put that in a method in your singleton class called Refresh() or something.

     

    Might not be the best solution though, i usually avoid singleton datacontext because of the weirdness with updates..

  • Thomas Dolberg 74 posts 95 karma points
    Dec 02, 2011 @ 11:05
    Thomas Dolberg
    0

    Hi,

     

    I cache my singleton datacontext which is then refreshed when a document is created, deleted, published, unpublished or moved to trash:

     

        using System;

        using System.Web;

        using umbraco.presentation;

        using umbraco.presentation.preview;

        using umbraco.Linq.Core.Node;

     

        using umbraco.cms.businesslogic.web;

     

        publicpartialclassLinqDataContext

        {

            private static object createContextLock = new object();

            public static LinqDataContext Instance

            {

                get

                {

                    if (HttpContext.Current != null)

                    {

                        var context = HttpContext.Current.Cache["LinqDataContext"] as LinqDataContext;

                        if (context == null)

                        {

                            lock (createContextLock)

                            {

                                if (context == null)

                                {

                                    context = new LinqDataContext();

                                    HttpContext.Current.Cache["LinqDataContext"] = context;

     

                                    //to ensure the cache items are always fresh, we clear the context when changes are made to a document

                                    Document.AfterNew += new EventHandler<umbraco.cms.businesslogic.NewEventArgs>(Document_AfterNew);

                                    Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);

                                    Document.AfterUnPublish += new Document.UnPublishEventHandler(Document_AfterUnPublish);

                                    Document.AfterDelete += new Document.DeleteEventHandler(Document_AfterDelete);

                                    Document.AfterMoveToTrash += new Document.MoveToTrashEventHandler(Document_AfterMoveToTrash);

     

                                }

                            }

                        }

                        return context;

                    }

                    else

                        return new LinqDataContext();

                }

            }

     

            static void Document_AfterMoveToTrash(Document sender, umbraco.cms.businesslogic.MoveToTrashEventArgs e)

            {

                if (HttpContext.Current != null)

                    HttpContext.Current.Cache.Remove("LinqDataContext");

            }

     

            static void Document_AfterDelete(Document sender, umbraco.cms.businesslogic.DeleteEventArgs e)

            {

                if (HttpContext.Current != null)

                    HttpContext.Current.Cache.Remove("LinqDataContext");

            }

     

            static void Document_AfterUnPublish(Document sender, umbraco.cms.businesslogic.UnPublishEventArgs e)

            {

                if (HttpContext.Current != null)

                    HttpContext.Current.Cache.Remove("LinqDataContext");

            }

     

            static void Document_AfterPublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)

            {

                if (HttpContext.Current != null)

                    HttpContext.Current.Cache.Remove("LinqDataContext");

            }

     

            static void Document_AfterNew(object sender, umbraco.cms.businesslogic.NewEventArgs e)

            {

                if (HttpContext.Current != null)

                    HttpContext.Current.Cache.Remove("LinqDataContext");

            }

        }

  • Scott Beveridge 4 posts 24 karma points
    Dec 07, 2011 @ 22:50
    Scott Beveridge
    0

    Thanks this works

     

     

  • Daniil 10 posts 31 karma points
    Jan 11, 2012 @ 14:20
    Daniil
    0

    Thanks, this topic helped me. Had the same problem.

    By the way, would simillar way work in load balanced environment? Do I have to find a way to explicitly ask another machines to remove cached data context?

  • Henri Toivonen 77 posts 111 karma points
    Jan 11, 2012 @ 14:23
    Henri Toivonen
    0

    The machines are not aware of eachothers cache, or rather, each application pool only knows what it is holding. Cache, Session, etc, is per app pool.

Please Sign in or register to post replies

Write your reply to:

Draft