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

Drupal 8 Cache contexts

By editor , 24 April, 2017
Question

How to use drupal 8 cache contexts ?

Drupal 8 shiped with a powerfull cache API able to set cache according to a context. correct usage of this cache improve the site preformence greatly.

https://www.drupal.org/docs/8/api/cache-api/cache-contexts
https://www.drupal.org/node/2451661


Cache contexts

cookies:NAME
headers:NAME
ip
languages:TYPE
request_format
route.book_navigation
route.menu_active_trails:MENU_NAME
route.name
session.exists
theme
timezone
url.path.is_front
url.path.parent
url.query_args:KEY
url.query_args.pagers:PAGER_ID
url.site
user.is_super_user
user.node_grants:OPERATIONS
user.permissions
user.roles:ROLE

Example 1 - URL :

$output['#cache']['max-age'] = 3600; // 3600 = 1h
$output['#cache']['contexts'] = ['url'];

Example 2 - User :

$output['#cache']['max-age'] = 3600; // 3600 = 1h
$output['#cache']['contexts'] = ['user'];

 

 

Tags

  • Drupal 8
  • Code
  • Cache
  • Add new comment
Drupal 8 Cache API
Drupal 8 Tags based cache system
Cache Drupal 8 Render Arrays

Comments1

Plain text

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

keltan (not verified)

9 years 6 months ago

Great Post

I'm very inspired with this weblog. this indicates it literally does not have any dietary cost and consuming it virtually makes your body use up stored nutrients to method it. last time I visit write my essay this website also awesome Honey then again has many useful nutrients, enzymes, and antioxidants; uncooked honey is first-class as standard honey processing

 

 

  • Reply

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