Streamlines how Template cache gets invalidated - ensures Save() is called on templates where it was needed,
cache now gets invalidated on events.
This commit is contained in:
@@ -5,6 +5,7 @@ using umbraco.cms.businesslogic;
|
||||
using umbraco.cms.businesslogic.macro;
|
||||
using umbraco.cms.businesslogic.member;
|
||||
using System.Linq;
|
||||
using umbraco.cms.businesslogic.template;
|
||||
|
||||
namespace Umbraco.Web.Cache
|
||||
{
|
||||
@@ -17,9 +18,15 @@ namespace Umbraco.Web.Cache
|
||||
{
|
||||
if (UmbracoSettings.UmbracoLibraryCacheDuration <= 0) return;
|
||||
|
||||
//Bind to template events
|
||||
|
||||
Template.AfterSave += TemplateAfterSave;
|
||||
Template.AfterDelete += TemplateAfterDelete;
|
||||
|
||||
//Bind to macro events
|
||||
|
||||
Macro.AfterSave += MacroAfterSave;
|
||||
Macro.AfterDelete += MacroAfterDelete;
|
||||
|
||||
//Bind to member events
|
||||
|
||||
@@ -37,6 +44,36 @@ namespace Umbraco.Web.Cache
|
||||
MediaService.Trashing += MediaServiceTrashing;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes cache for template
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
static void TemplateAfterDelete(Template sender, DeleteEventArgs e)
|
||||
{
|
||||
DistributedCache.Instance.RemoveTemplateCache(sender.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refresh cache for template
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
static void TemplateAfterSave(Template sender, SaveEventArgs e)
|
||||
{
|
||||
DistributedCache.Instance.RefreshTemplateCache(sender.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush macro from cache
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
static void MacroAfterDelete(Macro sender, DeleteEventArgs e)
|
||||
{
|
||||
DistributedCache.Instance.RemoveMacroCache(sender.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush macro from cache
|
||||
/// </summary>
|
||||
|
||||
@@ -17,6 +17,16 @@ namespace Umbraco.Web.Cache
|
||||
dc.Refresh(new Guid(DistributedCache.TemplateRefresherId), templateId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the cache amongst servers for a template
|
||||
/// </summary>
|
||||
/// <param name="dc"></param>
|
||||
/// <param name="templateId"></param>
|
||||
public static void RemoveTemplateCache(this DistributedCache dc, int templateId)
|
||||
{
|
||||
dc.Remove(new Guid(DistributedCache.TemplateRefresherId), templateId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the cache amongst servers for all pages
|
||||
/// </summary>
|
||||
@@ -75,5 +85,15 @@ namespace Umbraco.Web.Cache
|
||||
{
|
||||
dc.Refresh(new Guid(DistributedCache.MacroCacheRefresherId), macroId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the cache amongst servers for a macro item
|
||||
/// </summary>
|
||||
/// <param name="dc"></param>
|
||||
/// <param name="macroId"></param>
|
||||
public static void RemoveMacroCache(this DistributedCache dc, int macroId)
|
||||
{
|
||||
dc.Remove(new Guid(DistributedCache.MacroCacheRefresherId), macroId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ namespace Umbraco.Web.Cache
|
||||
{
|
||||
public class TemplateCacheRefresher : ICacheRefresher
|
||||
{
|
||||
private const string TemplateCacheKey = "template";
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
@@ -33,12 +35,18 @@ namespace Umbraco.Web.Cache
|
||||
|
||||
public void Refresh(int id)
|
||||
{
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate(id);
|
||||
RemoveFromCache(id);
|
||||
}
|
||||
|
||||
public void Remove(int id)
|
||||
{
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate(id);
|
||||
RemoveFromCache(id);
|
||||
}
|
||||
|
||||
private void RemoveFromCache(int id)
|
||||
{
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(
|
||||
string.Format("{0}{1}", TemplateCacheKey, id));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,19 +46,7 @@ namespace Umbraco.Web
|
||||
}
|
||||
|
||||
public const string PartialViewCacheKey = "Umbraco.Web.PartialViewCacheKey";
|
||||
private const string TemplateCacheKey = "template";
|
||||
|
||||
/// <summary>
|
||||
/// Clears the cache for a template
|
||||
/// </summary>
|
||||
/// <param name="cacheHelper"></param>
|
||||
/// <param name="templateId"></param>
|
||||
public static void ClearCacheForTemplate(this CacheHelper cacheHelper, int templateId)
|
||||
{
|
||||
cacheHelper.ClearCacheByKeySearch(
|
||||
string.Format("{0}{1}", TemplateCacheKey, templateId));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Clears the library cache for media
|
||||
/// </summary>
|
||||
|
||||
@@ -109,12 +109,6 @@ namespace Umbraco.Web.WebServices
|
||||
{
|
||||
t.Save();
|
||||
|
||||
// Clear cache in rutime
|
||||
if (UmbracoSettings.UseDistributedCalls)
|
||||
DistributedCache.Instance.RefreshTemplateCache(t.Id);
|
||||
else
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate(t.Id);
|
||||
|
||||
return Success(ui.Text("speechBubbles", "templateSavedText"), ui.Text("speechBubbles", "templateSavedHeader"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -11,6 +11,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Web;
|
||||
using Umbraco.Web.Cache;
|
||||
using umbraco.DataLayer;
|
||||
using umbraco.BusinessLogic;
|
||||
using umbraco.IO;
|
||||
@@ -561,7 +562,7 @@ namespace umbraco
|
||||
[Obsolete("Use ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate instead")]
|
||||
public static void ClearCachedTemplate(int templateID)
|
||||
{
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate(templateID);
|
||||
DistributedCache.Instance.RefreshTemplateCache(templateID);
|
||||
}
|
||||
|
||||
public template(String templateContent)
|
||||
|
||||
@@ -448,7 +448,7 @@ namespace umbraco.presentation.webservices
|
||||
return "false";
|
||||
}
|
||||
|
||||
[Obsolete("This method has been superceded by the REST service /Umbraco/RestServices/SaveFile/SaveTemplate which is powered by the SafeFileController.")]
|
||||
[Obsolete("This method has been superceded by the REST service /Umbraco/RestServices/SaveFile/SaveTemplate which is powered by the SaveFileController.")]
|
||||
[WebMethod]
|
||||
public string SaveTemplate(string templateName, string templateAlias, string templateContents, int templateID, int masterTemplateID)
|
||||
{
|
||||
@@ -466,13 +466,7 @@ namespace umbraco.presentation.webservices
|
||||
|
||||
_template.Save();
|
||||
|
||||
retVal = "true";
|
||||
|
||||
// Clear cache in rutime
|
||||
if (UmbracoSettings.UseDistributedCalls)
|
||||
DistributedCache.Instance.RefreshTemplateCache(_template.Id);
|
||||
else
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate(_template.Id);
|
||||
retVal = "true";
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -426,15 +426,10 @@ namespace umbraco.presentation.webservices
|
||||
tp.MasterTemplate = masterTemplateID;
|
||||
tp.Design = templateContents;
|
||||
|
||||
tp.Save();
|
||||
|
||||
retVal = "true";
|
||||
|
||||
// Clear cache in rutime
|
||||
if (UmbracoSettings.UseDistributedCalls)
|
||||
DistributedCache.Instance.RefreshTemplateCache(tp.Id);
|
||||
else
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate(tp.Id);
|
||||
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
@@ -95,7 +95,6 @@ namespace umbraco.webservices.templates
|
||||
template.Text = carrier.Name;
|
||||
template.Design = carrier.Design;
|
||||
template.Save();
|
||||
clearCachedTemplate(template);
|
||||
return template.Id;
|
||||
}
|
||||
|
||||
@@ -120,9 +119,6 @@ namespace umbraco.webservices.templates
|
||||
template.Text = carrier.Name;
|
||||
template.Design = carrier.Design;
|
||||
template.Save();
|
||||
|
||||
|
||||
clearCachedTemplate(template);
|
||||
}
|
||||
|
||||
[WebMethod]
|
||||
@@ -206,14 +202,5 @@ namespace umbraco.webservices.templates
|
||||
}
|
||||
}
|
||||
|
||||
private void clearCachedTemplate(cms.businesslogic.template.Template cachedTemplate)
|
||||
{
|
||||
// Clear cache in rutime
|
||||
if (UmbracoSettings.UseDistributedCalls)
|
||||
DistributedCache.Instance.RefreshTemplateCache(cachedTemplate.Id);
|
||||
else
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate(cachedTemplate.Id);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user