The TemplateCacheService provides a ton of helpful methods to take care of this for you.
While you can explicitly tell Craft to invalidate certain caches (by element ID, etc.) if you need to, I prefer to use includeCriteriaInTemplateCaches()
and includeElementInTemplateCaches()
to register the appropriate element(s)/criteria with the template cache, so that Craft takes care of cache invalidation according to its own internal logic:
$myCrtieria = ... ; // (some ElementCriteriaModel)
$cacheService = craft()->getComponent('templateCache', false);
if ($cacheService)
{
$cacheService->includeCriteriaInTemplateCaches($myCriteria);
}
...
p.s. Unless you really need to render HTML via a Twig hook, I strongly suggest refactoring to use a custom Template Variable instead. Doing so is more consistent with Craft's own internal practice. Twig hooks are typically used to change context variables and perform other logic that is specific to changing how Twig renders templates. For doing content-related business logic (e.g. fetching data from the database and rendering it into templates), using Craft's provided structures will save you trouble down the road.