Define elements that bust cache on save I wrote a little plugin that reads some entries (Events) from my events section, orders them in a calendar-like structure and returns them to be displayed in a template. (I couldn't do this twig-only)

Now, I would like to use {% cache %} on the calendar. I know that Craft normally busts the template cache when updating/adding an element if it detects, that those elements were used in a cached template.

Now as I am fetching the elements in my custom plugin with 

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);

I assume, that Craft can't know when to bust the cached output of the plugin.

Is there a way (with the TemplateCacheService for example) to make sure that this cache also gets busted when saving/updating one of my events?

I wonder if I can do this just in twig or if I would have to put a hook before the element gets saved? How does it relate to the cache key?

Any hints, which direction to take?

Thank you,

Robert



Update:

Okay, I'll provide some more details:

My Plugin looks (simplified) like this:

// MyPluginVariable.php: 
public function getMonthWithEvents($month, $year)
{
    ...
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;section = 'events';
    $criteria-&gt;date = ['and', '&gt;='.$monthStart, '&lt;='.$monthEnd];
    $eventsInMonth = $criteria-&gt;find();

    $month = [];

    // Put events in month array with dates etc....

    return $month;
}


In the twig template, I do this:

// agenda.twig
...

{% cache %}

    {% set month = craft.migrosAgenda.getMonthWithEvents(10, 2016) %}

    {% for day in month %}
        ...
        &lt;div class="day"&gt;
            {{ day.date|date('d.M.Y') }}
            {{ day.event.title }}
            {{ day.event.date }}
        &lt;/div&gt;
    {% endfor %}

{% endcache %}
Hard to know for sure without seeing some plugin code, but my guess is you'll want to use the TemplateCacheService's includeElementInTemplateCaches similar to how BaseTemplate's _includeElementInTemplateCaches does.