Last active
August 29, 2015 14:19
-
-
Save glompix/4d3b05ed666849f860ff to your computer and use it in GitHub Desktop.
Fix cache clear on remote publish in Sitecore language fallback module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Item GetFallbackItem(this Item item) | |
{ | |
Assert.IsNotNull(item, "item cannot be null"); | |
var success = false; | |
int iterations = 0, maxIterations = 5; | |
while (iterations++ < maxIterations) | |
{ | |
var fallbackLang = item.Language.GetFallbackLanguage(item.Database); | |
if (fallbackLang != null && | |
!fallbackLang.Name.IsNullOrEmpty() && | |
!fallbackLang.Equals(item.Language)) | |
{ | |
item = item.Database.GetItem(item.ID, fallbackLang, Version.Latest); | |
if (item != null && item.Versions.Count > 0) | |
{ | |
success = true; | |
break; | |
} | |
} | |
} | |
return success ? item : null; | |
} | |
public static bool HasFallbackVersion(this Item item) | |
{ | |
if (item.Versions.Count > 0) | |
{ | |
return true; | |
} | |
var fallbackItem = item.GetFallbackItem(); | |
return fallbackItem != null && fallbackItem.Versions.Count > 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void ClearFallbackCaches(PublishEndRemoteEvent publishArgs, Database database) | |
{ | |
Assert.ArgumentNotNull(publishArgs, "publishArgs"); | |
Assert.ArgumentNotNull(database, "database"); | |
var cache = _fallbackValuesCaches[database.Name]; | |
var ignore_cache = _fallbackIgnoreCaches[database.Name]; | |
var rootItemId = new ID(publishArgs.RootItemId); | |
var itemIds = new List<string>(256); | |
itemIds.Add(rootItemId.ToString()); | |
if (publishArgs.Deep) | |
{ | |
var rootItem = database.GetItem(rootItemId); | |
if (rootItem != null) | |
{ | |
using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext()) | |
{ | |
// get all items in medialibrary | |
var query = context.GetQueryable<SearchResultItem>().Where(i => i.Paths.Contains(rootItemId)); | |
foreach (var descendant in query) | |
{ | |
itemIds.Add(descendant.ItemId.ToString()); | |
} | |
} | |
} | |
} | |
foreach (var itemId in itemIds) | |
{ | |
if (cache != null) | |
{ | |
cache.RemoveKeysContaining(itemId.ToUpperInvariant()); | |
} | |
if (ignore_cache != null) | |
{ | |
ignore_cache.RemoveKeysContaining(itemId.ToUpperInvariant()); | |
} | |
} | |
} | |
private string SanitizePath(string path) | |
{ | |
var parts = path.Split('/'); | |
return string.Join("/", parts.Select(p => p.Contains("-") ? ("#" + p + "#") : p)); | |
} | |
#endregion | |
#region Initializers | |
private void InitializeEventHandlers(Database database) | |
{ | |
var dataEngine = database.Engines.DataEngine; | |
dataEngine.DeletedItem += DataEngine_DeletedItem; | |
dataEngine.SavedItem += DataEngine_SavedItem; | |
dataEngine.RemovedVersion += DataEngine_RemoveVersion; | |
EventManager.Subscribe<PublishEndRemoteEvent>(@event => ClearFallbackCaches(@event, database)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment