1

I'm learning the new caching system. For testing purposes I have turned off Page Cache and Dynamic Page Cache.

I've set a custom block with the following:

<some code>
$build_array = $view_builder->view($referenced_entity, self::VIEW_MODE, self::LANG_CODE);

$build_array['#cache']['contexts'][] = "route";
$build_array['#cache']['keys'] = ['mymodule_panels_blocks','taxonomy_term', $term->id()];

This is for displaying an eck entity with an image that is attached to a taxonomy term.

The expected behavior is that when I point the term to a different eck entity, the taxonomy term is invalidated.

When I check the full list of tags (using http.response.debug_cacheability_headers: true) I see my taxonomy term listed (taxonomy_term:209).

So it would seem I'm all set up. Yet when I change the eck entity to a new one, click Save, then click View, I see the old image still.

When I flush the render cache, I see my new image.

Isn't the invalidation of the taxonomy term automatic when the term is saved? Or do I have to write a hook to invalidate the taxonomy term when it changes?

3

Yes, the invalidation is automatic. From the debugging you have done so far you only know that the tag is somewhere on the page, but you don't know where it is and obviously it is not where you want it to be. Look in the database table cache_render. Clear the cache, so that it is emptied. Now request the page with the block and check for all cached elements the column tags.

By the way, you don't need to set cache keys on a block, because blocks do this automatically and for the problem here it's confusing, because the taxonomy id you set in the keys is not invalidated.

  • Thank you. With your help I learned that: • cache_render is easier to use than checking the tags that get to the header through the browser • keys do not need to be set for blocks; it's already handled by the builder's view method • I saw that the view method automatically gave me a tag for the referenced entity but I was missing the tag for the taxonomy term. Adding this did the trick $build_array['#cache']['tags'][] = $term->getEntityTypeId() . ':' .$term->id(); I've tested with no cache module, with Page Cache and with Dynamic Page Cache. All is working well. – André May 5 '17 at 18:13
  • 2
    Great, btw. there is a method to get the cache tag from an entity: $term->getCacheTags() – 4k4 May 5 '17 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.