How can I get the same element ids that the DeleteStaleTemplateCaches Task ends up with before the element has saved? I recently made a plugin that tries to keep the existing cache warm. It does this by listening to the elements.onBeforeSaveElement event, grabbing the id of that element and finding all the paths in the craft_templatecaches table then calls them with Guzzle.

The part I'm struggling with is the element id - when the event fires I just get the id of the element that first fired, in most cases the Entry. How do I ensure I get all of the correct cacheIds from the craft_templatecacheelements table? I only have the one element id, is there a way of ensuring I have all of the involved element ids?

I tried getting element ids from craft_relations by sourceId but still don't seem to be able to replicate the same kind of behaviour that the native cache clearing does. When I save an entry I get a bunch of element ids in the DeleteStaleTemplateCaches Task settings but can't find a way to get that same set of ids before the element saves.

I feel like I'm missing something fundamental ...
This is not much of an answer but basically Brad was right - I just needed to replicate the logic that the DeleteStaleTemplateCachesTask does in its getTotalSteps() and runStep() methods.

This allows you to work out exactly what caches will be just about to be deleted. I'm not going to post the code here as it is literally a copy/paste from the DeleteStaleTemplateCachesTask.