I have a block controller that leverages the Find Index to fetch data that is then passed into a view. All of this works and the block is functioning perfectly.
I want to wrap the Find logic with `IObjectInstanceCache` so that the Find call is only done for the first load of this block. This should help speed up TTFB of the page in general, and feels like the right thing to do.
Whenever a content author adds a new page into the CMS, the Find index will be updated accordingly after some small amount of time. How can I reliably clear my IObjectInstanceCache (e.g., `_cache.Remove(key)`) after the Find index has been updated? Is there an event for Indexing or Indexed similar to Saving, Publishing, Creating, etc? Is it safe enough to tap into the Published event? I feel like there would be a race condition between Publishing completing and Find re-indexing.
I want to avoid re-architecting the block to fetch data via Ajax, as this data is used above-the-fold and it ideally needs to be available as soon as the page loads for optimal experience.
I believe that clearing custom caching from a content event is bad practice. Episerver already does this in the framework and we can hook into this by "depending" on the content cache keys.
I wrote a pretty long article about this some time ago. Basically your cached data will get a cache dependency on Episerver's normal cache key, and be cleared when the content is cleared.
This, however, works only on the content instances. There is no guarantee that Find will have updated the indexes instantly. So this will work with .GetContentResult(), but not necessarily with .GetResult().
You should be using ISynchronizedObjectInstanceCache which would handle the cache invalidation across the instances for you. In your specific case it might not be required, but it is generally a good practice to follow.
There is no such Find events, but what you can do is to have time out for your cache. While it is not the best thing to do, it is compromise between performance and correctness.
Using Episerver's caching engine was critical to this implementation as we were needing to do lots of lookups server side. Because of this, I reworked the logic to no longer utilize the Find indexing service and instead leveraged the Episerver API directly. Once results were found, I shoved them in the cache and used that for subsequent page loads. I tapped into the Published event and trigger the cache clearing from there. Works like a charm!