<p>This is not much of an answer but basically Brad was right - I just needed to replicate the logic that the <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/tasks/DeleteStaleTemplateCachesTask.php" rel="nofollow">DeleteStaleTemplateCachesTask</a> does in its <code>getTotalSteps()</code> and <code>runStep()</code> methods.</p>

<p>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.</p>

CC BY-SA 3.0
2016-01-29T09:48:09.743Z
2016-01-29T09:48:09.743Z
Josh Angell
<p>I recently made <a href="https://github.com/supercool/Cache-Monster" rel="nofollow">a plugin</a> that tries to keep the existing cache warm. It does this by listening to the <code>elements.onBeforeSaveElement</code> event, grabbing the id of that element and finding all the paths in the <code>craft_templatecaches</code> table then calls them with Guzzle.</p>

<p>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 <code>cacheIds</code> from the <code>craft_templatecacheelements</code> table? I only have the one element id, is there a way of ensuring I have all of the involved element ids?</p>

<p>I tried getting element ids from <code>craft_relations</code> by <code>sourceId</code> 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.</p>

<p>I feel like I'm missing something fundamental ...</p>

CC BY-SA 3.0
2015-06-23T17:22:09.803Z
Wouldn't you just be following similar logic as the [DeleteStaleTemplateCachesTask](https://github.com/pixelandtonic/Craft-Release/blob/master/app/tasks/DeleteStaleTemplateCachesTask.php) does in `getTotalSteps()` and `runStep()`?
Brad Bell
CC BY-SA 3.0
2015-06-23T18:53:08.097Z
Clearly I was missing something fundamental. /facepalm
Josh Angell
CC BY-SA 3.0
2015-06-23T18:54:12.060Z
lol - be sure and add an official answer when you get it sorted. :)
Brad Bell
<plugin-development><cache>
How can I get the same element ids that the DeleteStaleTemplateCaches Task ends up with before the element has saved?
# How can I get the same element ids that the DeleteStaleTemplateCaches Task ends up with before the element has saved?

(Asked by: username_0 on 2015-06-23)

I recently made [a plugin](https://github.com/supercool/Cache-Monster) 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 ...

**Tags:** plugin-development, cache
> **Comments:**
> **username_1**: Wouldn't you just be following similar logic as the [DeleteStaleTemplateCachesTask](https://github.com/pixelandtonic/Craft-Release/blob/master/app/tasks/DeleteStaleTemplateCachesTask.php) does in `getTotalSteps()` and `runStep()`?  
> **username_0**: Clearly I was missing something fundamental. /facepalm  
> **username_1**: lol - be sure and add an official answer when you get it sorted. :)  

---

## 1 answers

### Answer by username_0 on 2016-01-29. Score: 1 | Accepted answer

This is not much of an answer but basically Brad was right - I just needed to replicate the logic that the [DeleteStaleTemplateCachesTask](https://github.com/pixelandtonic/Craft-Release/blob/master/app/tasks/DeleteStaleTemplateCachesTask.php) 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.


---

