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.
This commit is contained in:
@@ -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
|
||||
/// <returns></returns>
|
||||
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
|
||||
|
||||
@@ -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<DirectoryInfo, string, string[], RenderingEngine, bool> determineEngine =
|
||||
(directory, alias, extensions, renderingEngine) =>
|
||||
@@ -361,9 +366,11 @@ namespace Umbraco.Web.Routing
|
||||
LogHelper.Debug<PublishedContentRequest>("{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<PublishedContentRequest>("{0}Look for template alias=\"{1}\" (altTemplate)", () => tracePrefix, () => templateAlias);
|
||||
|
||||
var template = Template.GetByAlias(templateAlias);
|
||||
var template = Template.GetByAlias(templateAlias, true);
|
||||
_publishedContentRequest.Template = template;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<int>("select nodeId from cmsTemplate where alias = @alias", SqlHelper.CreateParameter("@alias", Alias)));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (!useCache)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Template(SqlHelper.ExecuteScalar<int>("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"));
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user