Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Cache Drupal 8 Render Arrays

By editor , 10 June, 2016
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

Tags

  • Drupal 8
  • Code
  • Cache
Cache Clearing and rebuilding methods for Drupal 8
Drupal 8 Tags based cache system
Create a simple module for Drupal 8 - Step 3 - Create Dynamic Page/ Block
Drupal 8 Cache API
Drupal 8 Cache contexts

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer