Industry News
Industry News

New method to invalidate twig templates

Change record status: 

Published (View all published change records)

Project: 

Drupal core

Introduced in branch: 8.6.x

Introduced in version: 8.6.0

Issues: 

#2752961: No reliable method exists for clearing the Twig cache

Description: 

Deploying Twig template changes in a multi webhead environment is not easy.

One strategy is sharing your file system and delete the twig files there (our current strategy). Sadly shared filesystems are slower.
The other strategy, not sharing files across servers, has the disadvantage, because deleting files would need need some form of sync between the servers. For more background information: #2752961: No reliable method exists for clearing the Twig cache.

As of #2752961: No reliable method exists for clearing the Twig cache we have a new method to invalidate Twig templates which is working across multiple webheads.

Before (not working on multiple webheads)

PhpStorageFactory::get('twig')->deleteAll();

After (fully working)

\Drupal::service('twig')->invalidate();

In case your deployment actually executed something like  drupal_flush_all_caches() it will start to work also on multi webhead environments.

Note: the new approach invalidates files but doesn't delete files across multiple webheads. If you are running Drupal in a multi webhead environment, you might want to delete stale files from the storage manually. To read more about a potential solution read comments #102–#123 at #2752961-102: No reliable method exists for clearing the Twig cache, which contains a couple of suggestions for potential strategies.
In other words: newly deployed Twig templates will be picked up as expected across all webheads (as long as \Drupal::service('twig')->invalidate(); is called after code deployment). But if each webhead is doing its own (local) caching, then stale compiled Twig templates will remain there. So over time, this could accumulate to a significant size.

 

Source: Drupal.org

You May Also Like

March 23, 2017
February 10, 2017
February 10, 2017
February 10, 2017
February 10, 2017
February 10, 2017
February 10, 2017
February 10, 2017
February 10, 2017
February 10, 2017

Get Aggregated Monthly Industry News