1

I want to clear custom cache when items at specific path are published. Here is what I tried so far.

Solution 1:

I created a custom event handler for publish:itemProcessed and publish:itemProcessed:remote which works fine for added and updated item but not deleted items. Before clearing cache I want to make sure item that is deleted is from specific path but Factory.GetDatabase(WebDatabaseName).GetItem(itemID) would give me null, think because it is already processed.

Solution 2:

I created custom processor CustomPublishProcessor and patched it in publish pipeline after "*[@type='Sitecore.Publishing.Pipelines.Publish.ProcessQueue, Sitecore.Kernel']" Here I am able to get ProcessedPublishingCandidates but clearing cache here won't be a good decision since it won't clear cache on remote CD servers. So I created one event handler on publish:end and publish:end:remote so that cache on all servers are cleared. I decided to pass some information to publish end event so that cache is getting cleared for perticular so I tried to set a cookie in CustomPublishProcessor and read it from handler.

Here is the code to set cookie

HttpCookie clearSolutionCacheCookie = new HttpCookie(ClearSolutionCookieKey);
clearSolutionCacheCookie.Value = "true";
HttpContext.Current.Response.Cookies.Add(clearSolutionCacheCookie);

it gives me HttpContext.Current as null.

I think I need to raise a custom event from CustomPublishProcessor to clear those cookie but I am not sure how to start.

I want experts' opinions here if I am over-doing customization? Am I missing something out of the box feature that does it. Can I achieve it by tweaking either of my solutions or I am completely on a wrong track.

1 Answer 1

1

If the problem with your first solution was that the item had already been removed and returns null, could you not try the same but hook into the begin publish event? If you check the web DB for the item prior to publishing it should not be null.

< event name="publish:begin" />

EDIT:

You can check what item is being published and then run your custom code if necessary.

var itemProcessedEventArgs = args as ItemProcessedEventArgs;
PublishItemContext context = itemProcessedEventArgs != null ? itemProcessedEventArgs.Context : null;

if (context != null && context.VersionToPublish != null && !context.ItemId.IsNull)
{
  Item beingPublished = Sitecore.Data.Database.GetDatabase("web").GetItem(context.ItemId);
  
  if (beingPublished.ID.ToString() == "{12345678-1234-1234-1234-123456789012}")
  {
    // Clear custom cach
  }
}
3
  • I don't want to update cache on every publish, I want to make sure that items that is being updated is from specific location. On publish:begin I can't be sure that item will be skipped or updated. Please correct me if I am wrong.
    – Imad
    Commented Jan 30, 2022 at 15:43
  • Please see updated answer above, hopefully that answers your question? Apologies if I misunderstood. Commented Jan 30, 2022 at 16:10
  • Thank you for clarification. I will give it a try and confirm.
    – Imad
    Commented Jan 30, 2022 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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