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....
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..
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?
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
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..
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");
}
}
Thanks this works
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?
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.
is working on a reply...