From a9d6c7872a1a700aaf4d263ee78cd764db9db400 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Wed, 24 Oct 2012 11:02:36 +0500 Subject: [PATCH] Changed the PublishedContentRequestBuild to use the cached template lookup instead of going to the db each time, added caching to the business logic template class's GetByAlias method. All of this will save db queries for each request meaning even faster rendering. Fixed GetTemplateAlias extension method to use the correct Template class with caching. --- src/Umbraco.Web/PublishedContentExtensions.cs | 5 +-- .../Routing/PublishedContentRequestBuilder.cs | 15 ++++++--- .../businesslogic/template/Template.cs | 33 ++++++++++--------- .../templates/templateService.cs | 2 +- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index c7820e8766..abad9afdb4 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -11,6 +11,7 @@ using Umbraco.Web.Routing; using umbraco; using umbraco.cms.businesslogic; using Umbraco.Core; +using umbraco.cms.businesslogic.template; namespace Umbraco.Web { @@ -52,8 +53,8 @@ namespace Umbraco.Web /// public static string GetTemplateAlias(this IPublishedContent doc) { - var template = new template(doc.TemplateId); - return template.TemplateAlias; + var template = Template.GetTemplate(doc.TemplateId); + return template.Alias; } #region Search diff --git a/src/Umbraco.Web/Routing/PublishedContentRequestBuilder.cs b/src/Umbraco.Web/Routing/PublishedContentRequestBuilder.cs index 32854c1769..e8cfbd856b 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequestBuilder.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequestBuilder.cs @@ -43,7 +43,12 @@ namespace Umbraco.Web.Routing return; } + //NOTE: Not sure how the alias is actually saved with a space as this shouldn't ever be the case? + // but apparently this happens. I think what should actually be done always is the template alias + // should be saved using the ToUmbracoAlias method and then we can use this here too, that way it + // it 100% consistent. I'll leave this here for now until further invenstigation. var templateAlias = _publishedContentRequest.Template.Alias.Replace(" ", string.Empty); + //var templateAlias = _publishedContentRequest.Template.Alias.ToUmbracoAlias(StringAliasCaseType.PascalCase); Func determineEngine = (directory, alias, extensions, renderingEngine) => @@ -361,9 +366,11 @@ namespace Umbraco.Web.Routing LogHelper.Debug("{0}Look for template id={1}", () => tracePrefix, () => templateId); if (templateId > 0) - { - //NOTE: This will throw an exception if the template id doesn't exist, but that is ok to inform the front end. - var template = new Template(templateId); + { + //NOTE: don't use the Template ctor as the result is not cached... instead use this static method + var template = Template.GetTemplate(templateId); + if (template == null) + throw new InvalidOperationException("The template with Id " + templateId + " does not exist, the page cannot render"); _publishedContentRequest.Template = template; } } @@ -371,7 +378,7 @@ namespace Umbraco.Web.Routing { LogHelper.Debug("{0}Look for template alias=\"{1}\" (altTemplate)", () => tracePrefix, () => templateAlias); - var template = Template.GetByAlias(templateAlias); + var template = Template.GetByAlias(templateAlias, true); _publishedContentRequest.Template = template; } diff --git a/src/umbraco.cms/businesslogic/template/Template.cs b/src/umbraco.cms/businesslogic/template/Template.cs index 992c8d6d16..fd9e9eda63 100644 --- a/src/umbraco.cms/businesslogic/template/Template.cs +++ b/src/umbraco.cms/businesslogic/template/Template.cs @@ -459,16 +459,23 @@ namespace umbraco.cms.businesslogic.template } } - public static Template GetByAlias(string Alias) + public static Template GetByAlias(string Alias, bool useCache = false) { - try - { - return new Template(SqlHelper.ExecuteScalar("select nodeId from cmsTemplate where alias = @alias", SqlHelper.CreateParameter("@alias", Alias))); - } - catch - { - return null; - } + if (!useCache) + { + try + { + return new Template(SqlHelper.ExecuteScalar("select nodeId from cmsTemplate where alias = @alias", SqlHelper.CreateParameter("@alias", Alias))); + } + catch + { + return null; + } + } + + //return from cache instead + var id = GetTemplateIdFromAlias(Alias); + return id == 0 ? null : GetTemplate(id); } [Obsolete("Obsolete, please use GetAllAsList() method instead", true)] @@ -854,17 +861,11 @@ namespace umbraco.cms.businesslogic.template }); } - private void InvalidateCache() - { - Cache.ClearCacheItem(GetCacheKey(this.Id)); - } - private static string GetCacheKey(int id) { return UmbracoTemplateCacheKey + id; } - - + public static Template Import(XmlNode n, User u) { string alias = xmlHelper.GetNodeValue(n.SelectSingleNode("Alias")); diff --git a/src/umbraco.webservices/templates/templateService.cs b/src/umbraco.webservices/templates/templateService.cs index 7e7ded2263..7b5fbe524f 100644 --- a/src/umbraco.webservices/templates/templateService.cs +++ b/src/umbraco.webservices/templates/templateService.cs @@ -31,7 +31,7 @@ namespace umbraco.webservices.templates try { - template = cms.businesslogic.template.Template.GetByAlias(alias); + template = cms.businesslogic.template.Template.GetByAlias(alias, true); } catch (Exception) {