3

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.

  • Take a look in chapter 5.12 from installation manual. Have you added your fields there to be processed within publishEndResultBatch? – Peter Procházka Mar 22 '18 at 5:21
  • The processor isnt even firing, much less resolving which fields have changed. – Ethan Schofer Mar 22 '18 at 12:49
  • Also, Im not really sure how that helps. Im not looking for specific fields. Im looking to fire a processor. With the old style publishing, I would handle the publish:end event. But it seems like that event no longer fires? – Ethan Schofer Mar 23 '18 at 0:58
  • I am also facing the same issue, none of the publishing service, custom event or pipeline are triggering.In sitecore able to publish the item. How did you resolve this issue? – Thenmozhi Subramaniam Feb 13 '19 at 7:07
  • I didnt. I could not make it work. – Ethan Schofer Feb 13 '19 at 15:43
2

You have three options what to do after Sitecore Publishing Service is done:

  1. You can hook on publishEndResultBatch pipeline. You can then work with these results and update any third-party systems or features that may need to know about the changes to items.

  2. With the Sitecore Publishing Service you have new event publishingservice:publishend which you can use. There are no remote events anymore for publishing service as service is already done remotely. See chapter 4.7 from installation manual referenced below.

  3. When you Publish all Items, you can check Clear all data caches checkbox. This will clear the data level caches that contain a reference to the items that are published. See chapter 7.4 from installation manual referenced below. For your scenario this won't work as expected as you have implemented custom cache.

For your scenario, definitely the 1. option is best. Hook on publishEndResultBatch pipeline and then work with these results. Remove items from your cache by comparing ids with ids that come as a result of this pipeline.

More information can be found in documentation:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.