I have cached some items using the .Net MemoryCache
. I would like to clear that cache on publish. Previously I would hook into the publish item and especially the publish item remote events.
How do I do this when using the Sitecore Publishing Service
?
Stephen Pope seems to suggest that these events do not work anymore.
Based on @chorpo's comment I tried option 1:
I created a processor:
public class ClearBlogCacheOnPublishProcessor
{
private readonly string basePostCacheKey = "blog-posts";
private readonly string baseCategoryCacheKey = "blog-categories";
private CacheProvider Cache { get; set; }
public void Process(PublishEndResultBatchArgs args)
{
Cache = new CacheProvider();
var cachesToClear = GetCacheKeysToClear(args.Batch, args.TargetInfo.TargetDatabaseName);
foreach (var key in cachesToClear)
{
Cache.Invalidate(key);
}
}
private IEnumerable<string> GetCacheKeysToClear(ManifestOperationResult<ItemResult>[] batch, string databaseName)
{
var cachesToClear = new List<string>();
var database = Sitecore.Configuration.Factory.GetDatabase(databaseName);
var siteInfoList = Sitecore.Configuration.Factory.GetSiteInfoList();
foreach (ManifestOperationResult<ItemResult> result in batch)
{
var id = result.EntityId.ToID();
var publishedItem = TryGetItem(database, id);
var site = GetSite(publishedItem, siteInfoList);
var name = site.Name;
if (publishedItem?.Parent == null &&
(publishedItem.TemplateID != BlogDetailPageFolderConstants.TemplateId ||
publishedItem.TemplateID != BlogDetailPageConstants.TemplateId))
{
var cacheKey = $"{name}:{basePostCacheKey}";
if (!cachesToClear.Contains(cacheKey))
cachesToClear.Add(cacheKey);
}
if (publishedItem?.Parent == null && (publishedItem.TemplateID == BlogCategoryConstants.TemplateId))
{
var cacheKey = $"{name}:{baseCategoryCacheKey}";
if (!cachesToClear.Contains(cacheKey))
cachesToClear.Add(cacheKey);
}
}
return cachesToClear;
}
private Item TryGetItem(Database database, ID itemId)
{
try
{
return database.Items[itemId];
}
catch (Exception ex)
{
Log.Error(this.ToString(), ex, this);
}
return null;
}
public static SiteInfo GetSite(Item item, IEnumerable<SiteInfo> siteInfoList)
{
return siteInfoList.FirstOrDefault(siteInfo => item.Paths.FullPath.StartsWith(siteInfo.RootPath));
}
}
I patched this in, I think correctly - this is the snippet from ShowConfig:
<publishEndResultBatch argsType="Sitecore.Publishing.Service.Pipelines.BulkPublishingEnd.PublishEndResultBatchArgs" patch:source="Sitecore.Publishing.Service.config">
<processor type="Sitecore.Publishing.Service.Pipelines.BulkPublishingEnd.RaiseRemoteEvents, Sitecore.Publishing.Service">
<!--
The number maximum number of manifest steps for a publishing target below which Sitecore Remote Item Events are
added to the publishing target Event Queue table. Exceeding this threshold will raise cache clearing events instead.
!Warning: Too low a value could have a significant impact on the performance of the delivery instance.
-->
<param name="remoteEventCacheClearingThreshold">1000</param>
<param name="targetCacheClearHistory" ref="publishing.service/targetCacheClearHistory"/>
</processor>
<processor type="DD.Platform.Sc.Pipelines.PublishEndResultBatch.ClearBlogCacheOnPublishProcessor, DD.Platform.Sc" patch:source="Delphic.ClearBlogCache.config"/>
</publishEndResultBatch>
But this pipeline does not seem to fire. I but a breakpoint in the code an publish and nothing happens. Also, the items still seem to be in cache.