Is there a way to create a Sitecore custom cache which exists for the duration of an indexing operation. I would like to store frequently looked up relationships which can be re-used by different computed index field classes to avoid performing the same lookups multiple times. After indexing completes, this cache can be cleared.

  • The question could be generalised to be "is there a way to serve data from a cache while it is being rebuilt". Seems like a .NET caching library solution would be good for this kind of thing. Feb 9 at 23:09
  • I don't think anything general .NET is needed, but I might be missing your point? Sitecore has its own caching APIs which can be extended, my problem is about having some sort of "indexing context" onto which I can persist certain lookup objects. I could use the Sitecore cache for that, but not sure how I would clear it at the end of the indexing job. Feb 9 at 23:33
  • Ok I misunderstood. How about just use a memory cache? docs.microsoft.com/en-us/dotnet/api/… Feb 9 at 23:38
  • Yeah that would work also, but my problem is how to clear this memory cache after the indexing job has finished? Feb 9 at 23:40
  • 1
    Perhaps hook into the indexing:end event? Feb 10 at 1:40

That is possible:

  • Sitecore.ContentSearch.AbstractSearchIndex.PerformUpdate will raise indexing:start and indexing:end with index name that is being worked on.

The handler listening to start event could create a common cache using BaseCacheManager.GetNamedInstance with registerNewInstance parameter true (share the same cache instance by name solution wide).

  • Wherever you need that cache during indexing, it could be recovered the same way.
  • The second handler to deal with indexing, will free cache as soon as indexing ends.

Things to be aware of:

A) You might want to enable the logic for a few indexes only (add filter by name clause)

B) Use Sitecore role-driven config to apply it only for the indexing instance

C) Your data is desired to implement Sitecore.Caching.Interfaces.ISizeTrackable to let Sitecore track memory consumption. Otherwise, it is brute force calculation with little accuracy.

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.