From 16ac26d44928d21cccd077b7e46cfa63f8f1ee6e Mon Sep 17 00:00:00 2001 From: slace Date: Tue, 22 Sep 2009 11:50:46 +0000 Subject: [PATCH] DO NOT DOWNLOAD. DOWNLOAD LATEST STABLE FROM RELEASE TAB Improving linq to umbracos interaction with the umbraco memory cache more use of client dependency [TFS Changeset #59351] --- .../Core/Node/NodeAssociationTree.cs | 16 +++++--- .../Core/Node/NodeDataProvider.cs | 38 ++++++++++++++++++- .../src/umbraco.Linq/Core/Node/NodeTree.cs | 17 ++++++--- umbraco/presentation/library.cs | 31 ++++++++------- 4 files changed, 75 insertions(+), 27 deletions(-) diff --git a/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeAssociationTree.cs b/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeAssociationTree.cs index 226c36db64..9bc9879bea 100644 --- a/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeAssociationTree.cs +++ b/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeAssociationTree.cs @@ -12,6 +12,7 @@ namespace umbraco.Linq.Core.Node /// The type of the doc type base. public sealed class NodeAssociationTree : AssociationTree where TDocTypeBase : DocTypeBase, new() { + private object lockObject = new object(); private IEnumerable _nodes; internal NodeAssociationTree(IEnumerable nodes) @@ -50,12 +51,15 @@ namespace umbraco.Linq.Core.Node provider.CheckDisposed(); - var rawNodes = provider.Xml.Descendants("node") - .Where(x => (int)x.Attribute("id") == this.ParentNodeId) - .Single() - .Elements("node") - ; - this._nodes = provider.DynamicNodeCreation(rawNodes).Cast(); //drop is back to the type which was asked for + lock (lockObject) + { + var rawNodes = provider.Xml.Descendants("node") + .Where(x => (int)x.Attribute("id") == this.ParentNodeId) + .Single() + .Elements("node") + ; + this._nodes = provider.DynamicNodeCreation(rawNodes).Cast(); //drop is back to the type which was asked for + } } /// diff --git a/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeDataProvider.cs b/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeDataProvider.cs index f7e2738c00..528ea4abf8 100644 --- a/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeDataProvider.cs +++ b/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeDataProvider.cs @@ -6,6 +6,7 @@ using System.Linq.Expressions; using System.IO; using System.Xml.Linq; using System.Xml.Schema; +using System.Xml; namespace umbraco.Linq.Core.Node { @@ -20,6 +21,7 @@ namespace umbraco.Linq.Core.Node /// public sealed class NodeDataProvider : UmbracoDataProvider { + private object lockObject = new object(); private string _xmlPath; private Dictionary _trees; private bool _enforceSchemaValidation; @@ -27,13 +29,30 @@ namespace umbraco.Linq.Core.Node private const string UMBRACO_XSD_PATH = "umbraco.Linq.Core.Node.UmbracoConfig.xsd"; private Dictionary _knownTypes; + private bool _tryMemoryCache = false; + internal XDocument Xml { get { if (this._xml == null) { - this._xml = XDocument.Load(this._xmlPath); + if (this._tryMemoryCache) + { + var doc = content.Instance.XmlContent; + if (doc != null) + { + this._xml = XDocument.Load(new XmlNodeReader(doc)); + } + else + { + this._xml = XDocument.Load(this._xmlPath); + } + } + else + { + this._xml = XDocument.Load(this._xmlPath); + } if (this._enforceSchemaValidation) { @@ -87,6 +106,7 @@ namespace umbraco.Linq.Core.Node public NodeDataProvider() : this(umbraco.presentation.UmbracoContext.Current.Server.MapPath(umbraco.presentation.UmbracoContext.Current.Server.ContentXmlPath)) { + this._tryMemoryCache = true; } /// @@ -179,7 +199,10 @@ namespace umbraco.Linq.Core.Node var tree = new NodeTree(this); if (!this._trees.ContainsKey(attr)) { - this._trees.Add(attr, tree); //cache so it's faster to get next time + lock (lockObject) + { + this._trees.Add(attr, tree); //cache so it's faster to get next time + } } else { @@ -307,5 +330,16 @@ namespace umbraco.Linq.Core.Node return this._knownTypes; } } + + /// + /// Flushes the cache for this provider + /// + public void Flush() + { + this.CheckDisposed(); + + this._xml = null; + this._trees.Clear(); + } } } diff --git a/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeTree.cs b/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeTree.cs index ed9a1f5841..687edd0fdb 100644 --- a/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeTree.cs +++ b/LinqToUmbraco/src/umbraco.Linq/Core/Node/NodeTree.cs @@ -12,6 +12,8 @@ namespace umbraco.Linq.Core.Node /// The type of the doc type base. public sealed class NodeTree : Tree where TDocTypeBase : DocTypeBase, new() { + private object lockObject = new object(); + private NodeDataProvider _provider; private List _nodes; @@ -67,15 +69,18 @@ namespace umbraco.Linq.Core.Node this._nodes = new List(); var rawNodes = this._provider.Xml.Descendants("node").Where(x => ReflectionAssistance.CompareByAlias(typeof(TDocTypeBase), x)); - foreach (XElement n in rawNodes) + lock (lockObject) { - var dt = new TDocTypeBase(); - dt.LoadFromXml(n); + foreach (XElement n in rawNodes) + { + var dt = new TDocTypeBase(); + dt.LoadFromXml(n); - dt.IsDirty = false; - dt.Provider = this._provider; + dt.IsDirty = false; + dt.Provider = this._provider; - this._nodes.Add(dt); + this._nodes.Add(dt); + } } } return this._nodes.GetEnumerator(); diff --git a/umbraco/presentation/library.cs b/umbraco/presentation/library.cs index 25bb0258a2..7866b4f30d 100644 --- a/umbraco/presentation/library.cs +++ b/umbraco/presentation/library.cs @@ -1,5 +1,4 @@ using System; -using System.Data; using System.Globalization; using System.IO; using System.Net; @@ -23,7 +22,6 @@ using umbraco.cms.helpers; using umbraco.presentation.cache; using umbraco.scripting; using umbraco.DataLayer; -using umbraco.presentation.templateControls; using System.Web.Security; using umbraco.cms.businesslogic.language; @@ -1291,22 +1289,29 @@ namespace umbraco if (p != null) { - System.Web.UI.HtmlControls.HtmlGenericControl include = new System.Web.UI.HtmlControls.HtmlGenericControl("script"); - include.ID = key; - include.Attributes.Add("type", "text/javascript"); - include.Attributes.Add("src", url); - - if (p.Header != null) + if (ClientDependency.Core.Controls.ClientDependencyLoader.Instance == null) { - if (p.Header.FindControl(key) == null) + System.Web.UI.HtmlControls.HtmlGenericControl include = new System.Web.UI.HtmlControls.HtmlGenericControl("script"); + include.ID = key; + include.Attributes.Add("type", "text/javascript"); + include.Attributes.Add("src", url); + + if (p.Header != null) { - p.Header.Controls.Add(include); + if (p.Header.FindControl(key) == null) + { + p.Header.Controls.Add(include); + } + } + else + { + //This is a fallback in case there is no header + p.ClientScript.RegisterClientScriptInclude(p.GetType(), key, url); } } else { - //This is a fallback in case there is no header - p.ClientScript.RegisterClientScriptInclude(p.GetType(), key, url); + ClientDependency.Core.Controls.ClientDependencyLoader.Instance.RegisterDependency(url, ClientDependency.Core.ClientDependencyType.Javascript); } } } @@ -1317,7 +1322,7 @@ namespace umbraco /// public static void AddJquery() { - RegisterJavaScriptFile("jQuery", String.Format("{0}_client/ui/jquery.js", GlobalSettings.Path)); + RegisterJavaScriptFile("jQuery", String.Format("{0}/ui/jquery.js", GlobalSettings.ClientPath)); }