Cache Drupal 8 Render Arrays

来源:互联网 发布:淘宝网招聘求职平台 编辑:程序博客网 时间:2024/05/21 10:13
Question

Cacheability of render arrays.
How to Cache, clear cache (basend on time , tag) of a drupal 8 render array ?

Cache configuration of a renderer array (Just an exaple)

$render_array = [
  '#markup' => 'Time : ' . date("H:i:s"),
  '#cache' => [
    'keys' => ['entity_view''node', $node->id()],
    'contexts' => ['languages'],
    'tags' => ['node:' . $node->id()],
    'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
  ],

];

 

Example 1. Disable Cache of a render array.

$render_array = [
  '#markup' => 'Time : ' . date("H:i:s"),
  '#cache' => [
      'disabled' => TRUE,
      ],

  ];

Example 2. Time Based Cache.

$render_array = [
  '#markup' => 'Time : ' . date("H:i:s"),
  '#cache' => [
      'max_age'=> 0
//No cache (Like 'disabled' => TRUE,)
      'max_age'=> 60//60 seconds / 1 minute
      'max_age'=> 3600, //3600 seconds / 1 hour 
      ],
  ];

Example 3. Tags Based Cache.

$render_array = [
  '#markup' => 'Time : ' . date("H:i:s"),
  '#cache' => [
       'tags' => ['your_module' . ':' . 'your_tag'],
      ],

  ];

Link : https://www.drupal.org/developing/api/8/render/arrays/cacheability
API :  https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/group/theme_render/8.2.x

0 0
原创粉丝点击