Fixes: #U4-1981 - ensures template cache is properly refreshed amonsgst all servers

This commit is contained in:
Shannon Deminick
2013-03-22 02:08:55 +06:00
parent 9d6b38a1c1
commit ee2f2dfda0
6 changed files with 60 additions and 37 deletions

View File

@@ -16,7 +16,8 @@ namespace Umbraco.Core.Cache
public const string MemberCacheKey = "UL_GetMember";
public const string TemplateCacheKey = "template";
public const string TemplateFrontEndCacheKey = "template";
public const string TemplateBusinessLogicCacheKey = "UmbracoTemplateCache";
public const string UserCacheKey = "UmbracoUser";

View File

@@ -60,9 +60,12 @@ namespace Umbraco.Web.Cache
User.Deleting += UserDeleting;
//Bind to template events
//NOTE: we need to bind to legacy and new API events currently: http://issues.umbraco.org/issue/U4-1979
Template.AfterSave += TemplateAfterSave;
Template.AfterDelete += TemplateAfterDelete;
FileService.SavedTemplate += FileServiceSavedTemplate;
FileService.DeletedTemplate += FileServiceDeletedTemplate;
//Bind to macro events
@@ -241,6 +244,27 @@ namespace Umbraco.Web.Cache
#endregion
#region Template event handlers
/// <summary>
/// Removes cache for template
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void FileServiceDeletedTemplate(IFileService sender, Core.Events.DeleteEventArgs<ITemplate> e)
{
e.DeletedEntities.ForEach(x => DistributedCache.Instance.RemoveTemplateCache(x.Id));
}
/// <summary>
/// Refresh cache for template
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void FileServiceSavedTemplate(IFileService sender, Core.Events.SaveEventArgs<ITemplate> e)
{
e.SavedEntities.ForEach(x => DistributedCache.Instance.RefreshTemplateCache(x.Id));
}
/// <summary>
/// Removes cache for template
/// </summary>

View File

@@ -45,6 +45,7 @@ namespace Umbraco.Web.Cache
{
dc.Remove(new Guid(DistributedCache.TemplateRefresherId), templateId);
}
#endregion
#region Page cache

View File

@@ -50,8 +50,11 @@ namespace Umbraco.Web.Cache
private void RemoveFromCache(int id)
{
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(
string.Format("{0}{1}", CacheKeys.TemplateCacheKey, id));
ApplicationContext.Current.ApplicationCache.ClearCacheItem(
string.Format("{0}{1}", CacheKeys.TemplateFrontEndCacheKey, id));
ApplicationContext.Current.ApplicationCache.ClearCacheItem(
string.Format("{0}{1}", CacheKeys.TemplateBusinessLogicCacheKey, id));
}
}

View File

@@ -10,6 +10,7 @@ using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Web;
using Umbraco.Web.Cache;
using umbraco.DataLayer;
@@ -29,16 +30,11 @@ namespace umbraco
readonly StringBuilder _templateOutput = new StringBuilder();
// Cache
//static System.Web.Caching.Cache templateCache = System.Web.HttpRuntime.Cache;
private string _templateDesign = "";
int _masterTemplate = -1;
private string _templateName = "";
private string _templateAlias = "";
private const string CacheKey = "template";
#endregion
#region public properties
@@ -498,7 +494,7 @@ namespace umbraco
var tId = templateID;
var t = ApplicationContext.Current.ApplicationCache.GetCacheItem(
string.Format("{0}{1}", CacheKey, tId), () =>
string.Format("{0}{1}", CacheKeys.TemplateFrontEndCacheKey, tId), () =>
{
using (var templateData = SqlHelper.ExecuteReader("select nodeId, alias, master, text, design from cmsTemplate inner join umbracoNode node on node.id = cmsTemplate.nodeId where nodeId = @templateID", SqlHelper.CreateParameter("@templateID", templateID)))
{

View File

@@ -3,6 +3,8 @@ using System.Linq;
using System.Collections;
using System.Xml;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using umbraco.DataLayer;
using System.Text.RegularExpressions;
@@ -10,7 +12,6 @@ using System.IO;
using System.Collections.Generic;
using umbraco.cms.businesslogic.cache;
using umbraco.BusinessLogic;
using umbraco.IO;
using umbraco.cms.businesslogic.web;
namespace umbraco.cms.businesslogic.template
@@ -39,10 +40,8 @@ namespace umbraco.cms.businesslogic.template
public static readonly string UmbracoMasterTemplate = SystemDirectories.Umbraco + "/masterpages/default.master";
private static Hashtable _templateAliases = new Hashtable();
private static volatile bool _templateAliasesInitialized = false;
private static object templateCacheSyncLock = new object();
private static readonly string UmbracoTemplateCacheKey = "UmbracoTemplateCache";
private static object _templateLoaderLocker = new object();
private static Guid _objectType = new Guid(Constants.ObjectTypes.Template);
private static readonly object TemplateLoaderLocker = new object();
private static readonly Guid ObjectType = new Guid(Constants.ObjectTypes.Template);
private static readonly char[] NewLineChars = Environment.NewLine.ToCharArray();
#endregion
@@ -94,7 +93,6 @@ namespace umbraco.cms.businesslogic.template
if (!e.Cancel)
{
FlushCache();
base.Save();
FireAfterSave(e);
}
@@ -211,7 +209,7 @@ namespace umbraco.cms.businesslogic.template
SqlHelper.ExecuteNonQuery("Update cmsTemplate set alias = @alias where NodeId = " + this.Id, SqlHelper.CreateParameter("@alias", _alias));
_templateAliasesInitialized = false;
initTemplateAliases();
InitTemplateAliases();
}
}
@@ -402,7 +400,7 @@ namespace umbraco.cms.businesslogic.template
{
// CMSNode MakeNew(int parentId, Guid objectType, int userId, int level, string text, Guid uniqueID)
CMSNode n = CMSNode.MakeNew(-1, _objectType, u.Id, 1, name, Guid.NewGuid());
CMSNode n = CMSNode.MakeNew(-1, ObjectType, u.Id, 1, name, Guid.NewGuid());
//ensure unique alias
name = helpers.Casing.SafeAlias(name);
@@ -489,7 +487,7 @@ namespace umbraco.cms.businesslogic.template
public static List<Template> GetAllAsList()
{
Guid[] ids = CMSNode.TopMostNodeIds(_objectType);
Guid[] ids = CMSNode.TopMostNodeIds(ObjectType);
List<Template> retVal = new List<Template>();
foreach (Guid id in ids)
{
@@ -503,18 +501,18 @@ namespace umbraco.cms.businesslogic.template
{
alias = alias.ToLower();
initTemplateAliases();
InitTemplateAliases();
if (TemplateAliases.ContainsKey(alias))
return (int)TemplateAliases[alias];
else
return 0;
}
private static void initTemplateAliases()
private static void InitTemplateAliases()
{
if (!_templateAliasesInitialized)
{
lock (_templateLoaderLocker)
lock (TemplateLoaderLocker)
{
//double check
if (!_templateAliasesInitialized)
@@ -552,7 +550,7 @@ namespace umbraco.cms.businesslogic.template
{
//re-set the template aliases
_templateAliasesInitialized = false;
initTemplateAliases();
InitTemplateAliases();
//delete the template
SqlHelper.ExecuteNonQuery("delete from cmsTemplate where NodeId =" + this.Id);
@@ -740,33 +738,33 @@ namespace umbraco.cms.businesslogic.template
}
[Obsolete("Umbraco automatically ensures that template cache is cleared when saving or deleting")]
protected virtual void FlushCache()
{
// clear local cache
cache.Cache.ClearCacheItem(GetCacheKey(Id));
ApplicationContext.Current.ApplicationCache.ClearCacheItem(GetCacheKey(Id));
}
public static Template GetTemplate(int id)
{
return Cache.GetCacheItem<Template>(GetCacheKey(id), templateCacheSyncLock,
return ApplicationContext.Current.ApplicationCache.GetCacheItem(
GetCacheKey(id),
TimeSpan.FromMinutes(30),
delegate
{
try
() =>
{
return new Template(id);
}
catch
{
return null;
}
});
try
{
return new Template(id);
}
catch
{
return null;
}
});
}
private static string GetCacheKey(int id)
{
return UmbracoTemplateCacheKey + id;
return CacheKeys.TemplateBusinessLogicCacheKey + id;
}
public static Template Import(XmlNode n, User u)