0

I have a private method to publish an item to the Sitecore Web database programmatically. It's publishing the item to the Web database but not clearing the cache. I still see the data from the older version of the published item. I was under the impression that the Sitecore publishing module in Sitecore 9.3 version automatically clears the cache when the item is published.

Do I have to explicitly write a line of code to clear the cache or is there a better way to do this?

I did try to add this line but it did not work for me Sitecore.Caching.CacheManager.ClearAllCaches()

private void PublishItem(Sitecore.Data.Items.Item item)
{
    PublishOptions publishOptions = 
        new PublishOptions(item.Database,
        Database.GetDatabase("web"),
        PublishMode.SingleItem,
        item.Language,
        DateTime.Now)
    {
        UserName = "sitecore\\admin"
    };
    Publisher publisher = new Publisher(publishOptions);
    publisher.Options.RootItem = item;
    publisher.Options.Deep = true;
    publisher.PublishAsync();

    item.Publishing.ClearPublishingCache();
}
6
  • It looks like your scalability config has not been enabled properly. Does this work if you publish from content editor ? Commented Dec 8, 2021 at 18:27
  • You are doing an Async publish but are not waiting for the publish to finish before clearing the cache. Does this work if you wait before clearing cache?
    – Jeroen
    Commented Dec 8, 2021 at 18:45
  • Yes, the cache is cleared and the data is displayed on CD from the latest version when I published the item manually from CMS.
    – Asad Syed
    Commented Dec 8, 2021 at 18:54
  • Normally, whether you perform the publish manually or programmatically, there are Sitecore events that clears the cache. When you trigger the publish programmatically, check on jobs.aspx to see when the publish is completed. Once done, check the item Commented Dec 8, 2021 at 22:52
  • Where is the cache not being cleared? The same server or a separate CD server? If it’s not being cleared in a CD server then the event queue is not working properly. Also- how are you confirming the item is publishing to web but not clearing the cache? Are you doing an app pool recycle to verify?
    – jrap
    Commented Dec 9, 2021 at 0:23

2 Answers 2

1

This is how item caching works before hitting the database: enter image description here

For my Sitecore 9.3 scaled solution I use this helper to clear item's cache (after some item is changed programmatically):

public static class CacheHelper
{
    public static void ClearItemCache(this Database database,  ID itemId)
    {
        var prefetchCache = CacheManager.FindCacheByName<ID>("SqlDataProvider - Prefetch data(" + database + ")");
        if (prefetchCache != null)
        {
            prefetchCache.Remove(itemId);
        }
        CacheManager.GetItemCache(database).RemoveItem(itemId);
        database.Caches.ItemCache.RemoveItem(itemId);
        database.Caches.DataCache.RemoveItemInformation(itemId);
    }
}

Example of usage:

var db = Database.GetDatabase("web");
db.ClearItemCache(itemId);

But it is strange that Sitecore.Caching.CacheManager.ClearAllCaches() does not work in your case. Maybe the problem is not in caches. As @Jeroen mentioned it seems that you clear cache before publish is finished because you use PublishAsync. Try to use async/await or ContinueWith to be sure that your cache is clear when publishing is finished.

4
  • I downvoted as 1) that image is misleading (see my recent posts on the subject) and 2) it doesn’t address the issue the OP is facing. As Hishaam mentioned- their are remote events that handle the cache clearing. Triggering a publish programmatically is fine. We need to know if we’re taking about a single instance or scaled.
    – jrap
    Commented Dec 9, 2021 at 0:18
  • Thank you for downvote). 1) Image illustrates a cycle of storages where does the item come from when it is rendered or gets by Database.GetItem(). It is just for better understanding of caches workflow. 2) Read the title of issue carefully.
    – x3mxray
    Commented Dec 9, 2021 at 9:48
  • That's fair x3mxray. I still despise the image but yes your answer does address the question posed.
    – jrap
    Commented Dec 9, 2021 at 17:26
  • the correct order to clear caches manually would be prefetch, then data and then item. Also, your two item cache clears are redundant in your sample code.
    – jrap
    Commented Dec 11, 2021 at 2:57
0

As @Jeroen pointed out that we had to wait for the publish to complete before clearing the cache. I used PublishManager.WaitFor() method to wait for the publishing to complete. It published the item to CD and cleared the cache. Please see the updated code below.

private void PublishItem(Sitecore.Data.Items.Item item)
{
    PublishOptions publishOptions = 
        new PublishOptions(item.Database,
        Database.GetDatabase("web"),
        PublishMode.SingleItem,
        item.Language,
        DateTime.Now)
    {
        UserName = "sitecore\\admin"
    };
    publishOptions.RootItem = item;
    publishOptions.Deep = true;
    publishOptions.PublishRelatedItems = true;
    publishOptions.CompareRevisions = true;       

    var handle = PublishManager.Publish(new PublishOptions[] { publishOptions });
    PublishManager.WaitFor(handle);
    item.Publishing.ClearPublishingCache();
}

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.