Skip to content

Commit

Permalink
Hook publish:end:remote to clear delivery node cache. Fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
seankearney committed Aug 6, 2014
1 parent 052e277 commit b8489f1
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/Kernel/Data/FallbackValuesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Events;
using Sitecore.Globalization;
using Sitecore.Publishing;

namespace FieldFallback.Data
{
Expand Down Expand Up @@ -219,7 +221,7 @@ public virtual bool FieldContainsFallbackValue(Field field, Language language)
bool hasFallbackValue = (field.Value == fallbackValue);

Logger.Debug("{0}", hasFallbackValue);
return hasFallbackValue;
return hasFallbackValue;
}
finally
{
Expand Down Expand Up @@ -403,7 +405,7 @@ private void EnableSites()
/// <param name="database">The database.</param>
private void InitializeEventHandlers(Database database)
{
Logger.Info("Instatiating event handlers for database: {0}", database.Name);
Logger.Info("Instantiating event handlers for database: {0}", database.Name);
Sitecore.Data.Engines.DataEngine dataEngine = database.Engines.DataEngine;

// Hook into the Delete/Remove/Saved events to clear caches
Expand All @@ -425,13 +427,17 @@ private void InitializeEventHandlers(Database database)
dataEngine.CopiedItem += DataEngine_CopiedItem;
dataEngine.SavingItem += DataEngine_SavingItem;
dataEngine.SavedItem += DataEngine_SavedItem;
dataEngine.SavedItemRemote+=DataEngine_SavedItemRemote;
dataEngine.SavedItemRemote += DataEngine_SavedItemRemote;
dataEngine.CreatingItem += DataEngine_CreatingItem;
dataEngine.CreatedItem += DataEngine_CreatedItem;
dataEngine.AddingFromTemplate += DataEngine_AddingFromTemplate;
dataEngine.AddedFromTemplate += DataEngine_AddedFromTemplate;
dataEngine.AddingVersion += DataEngine_AddingVersion;
dataEngine.AddedVersion += DataEngine_AddedVersion;

// The delivery nodes must subscribe to the end publish event...
// we need to invalidate the cache of published items
Event.Subscribe("publish:end:remote", new System.EventHandler(this.OnPublishEndRemoteHandled));
}

private void DataEngine_RemoveVersion(object sender, ExecutedEventArgs<RemoveVersionCommand> e)
Expand Down Expand Up @@ -520,6 +526,37 @@ private void DataEngine_AddedVersion(object sender, ExecutedEventArgs<AddVersion
ExitDisabledState("Added Version of item '{0}' ('{1}:{2}')", e.Command.Item.Name, e.Command.Item.Language.Name, e.Command.Item.Version.Number);
}

private void OnPublishEndRemoteHandled(object sender, EventArgs args)
{
PublishEndRemoteEventArgs remoteArgs = args as PublishEndRemoteEventArgs;

bool deep = remoteArgs.Deep;
ID rootItemID = ID.Parse(remoteArgs.RootItemId);

// We can't rely on the name of the target database (as the publishing target name in the CM may not match a DB name in the CD environment)
// Iterate over each supported DB and try to get the item
foreach (string db in SupportedDatabaseNames)
{
Database publishingTarget = Sitecore.Configuration.Factory.GetDatabase(db);
if (publishingTarget != null)
{
Item publishedItem = publishingTarget.GetItem(rootItemID);

if (publishedItem != null)
{
if (deep)
{
Cache.RemoveTree(publishedItem);
}
else
{
Cache.RemoveItem(publishedItem);
}
}
}
}
}

/// <summary>
/// Disabled FieldFallback and logs the message to the Debug output
/// </summary>
Expand Down

0 comments on commit b8489f1

Please sign in to comment.