November 22, 2017 6 Min Read
_p]:text-2xl [&_blockquote_>_p]:not-italic [&_h2]:!mb-2 [&_li]:text-paragraph [&_p]:text-paragraph"> Drupal 8 has evolved in many ways over its predecessor. Some of the prominent features are the inclusion of the widely used views module into core, configuration management, and an easy to use translation service. However, today we are going to dive into one of the other major updates of Drupal 8 that provides a huge boost to performance and perceived performance: the cache layer. Caching in Drupal 8 has received a much needed overhaul. It has become highly customizable and dynamic, and now has the ability to cache individual blocks or items on a page. This means that we can control exactly when a block on a page has its cache cleared instead of clearing the entire page cache. Diving deeper into caching There are three main components to the cache layer in Drupal 8: Cache tags Cache contexts Cache max-age Cache tags Cache tags are for dependencies on data that are managed by Drupal, and are the easiest way to control cache. As an example, let’s consider a site with a news story content type and view page. This site has a list of three nodes that are displayed in a block on the homepage. How would the homepage cache (more specifically the news block cache) be cleared if we added a news story to the top of this list? The answer is cache tags! Cache tags are used to inform the system when the news block would need to be invalidated. The homepage might have cache tags specific to the page itself node:5, but this will not invalidate the entire homepage when a news story node:18 is added since the cache tag does not exist in the list of tags for the homepage. The news story block, on the other hand, would have specific tags that line up with the node ID’s that currently appear in the list node:16; node:17; node:18. So when we update or add a new news node, we will invalidate all caches that contain that particular cache tag. In this case node:18 tag will be invalidated, and thus the block that contains the tag node:18 will also be invalidated. Cache contexts Cache contexts are used when a dependency exists on an environment-specific object. Content is often reused across multiple pages and the cached object could be rendered differently based on the environment’s state. In this example, we will take a look at how cache contexts can be used and important considerations for developers, by modifying the default breadcrumb in Drupal 8. By default, the breadcrumb only shows the path up to and including the parent page. For many websites, we also need to display the current page title in the breadcrumb. Here we have altered it to add the current page title to the end: function theme_preprocess_breadcrumb(&$variables) { if (($node = \Drupal::routeMatch()->getParameter('node')) && $variables['breadcrumb']) { $variables['breadcrumb'][] = [ 'text' => $node->getTitle() ]; } } Now the following page http://home.com/page-level-1/page-level-2/page-level-3 will have the following breadcrumb: Home > Page Level 1 > Page Level 2 > Page Level 3 Great! We have the breadcrumb we are looking for. But we have not yet considered how this might have affected caching. Additionally at this point we have not changed anything to do with the caching of the breadcrumb. Drupal does not know how it should handle caching our new version of the breadcrumb and this can create some unexpected behaviours. If we look at the cache contexts, which can be configured to be sent along in the response header, we see the breadcrumb is cached on the context of the parent url: This means that once Page Level 3 is loaded, the breadcrumb is now cached based on the parent url. When a sibling page like Page Level 3-1 is loaded, the parent page has not changed, and thus the breadcrumb will be loaded from cache. This can give us unexpected behaviour due to the missing cache context. When running on a cold cache (a cache that has been cleared or rebuilt) and you load Page Level 3-1, the proper breadcrumb is displayed. Home > Page Level 1 > Page Level 2 > Page Level 3-1 When you go to the sibling page Page Level 3, you will notice that the previous breadcrumb is still showing: Home > Page Level 1 > Page Level 2 > Page Level 3-1 When we altered the breadcrumb we also needed to be aware of the cache settings on it. Since the breadcrumb is now unique to each specific page and not just to the parent page, we need to add the following to our preprocess_breadcrumb function: function theme_preprocess_breadcrumb(&$variables) { if (($node = \Drupal::routeMatch()->getParameter('node')) && $variables['breadcrumb']) { $variables['breadcrumb'][] = [ 'text' => $node->getTitle() ]; $variables['#cache']['contexts'][] = 'url.path'; } } With http.response.debug_cacheability_headers enabled, we can now see that the url.path context is appearing in our response header. This will then use the current path to cache the breadcrumb. Now, when running on a cold cache, we load page Page Level 3-1 and the proper breadcrumb is shown. When loading the sibling Level 3, the proper breadcrumb is now showing for it as well. Home > Page Level 1 > Page Level 2 > Page Level 3 This is a quick example of how cache contexts can alter the output of a page, and how developers must take this into consideration when working with Drupal 8. Cache max-age Cache max-age is used when there are time dependencies attached to the content. An example of a time dependence is the need to invalidate items when using feeds or timelines as the content changes periodically. For example, let’s say we are importing a feed into a block and we have the import set to every 30 minutes. We would want to add a max-age cache of 30 minutes to the block in order to invalidate it consistently after the feed is imported. Anytime your content has a dependency on time, you should consider checking or setting a Max-Age cache value. Big Pipe Drupal recently included an experimental module in core called Big Pipe. The Big Pipe concept is about loading page data as it becomes available instead of waiting for the entire response before showing anything. This will not affect the total response time, but will greatly reduce the perceived response time. The page will appear to load faster and portions of the content can be readable in less time. Below is a video showing the perceived speed of Big Pipe. Page data is displayed as it becomes ready, and placeholders are created for sidebar items that will be filled in as the data is available. This caching improvement applies to authenticated users viewing pages which have partially customized content. Wrap up This was a quick look into the new cache layer in Drupal 8. The cache layer provides three ways for us to declare how we want our feature or content to be cached. These components of the cache layer are a very important detail we need to take into account when developing for Drupal 8. The updates to the cache layer provide a very robust system and are a big stepping stone for Drupal 8’s performance. Don’t forget to take a closer look at the cache API and how it can help you store, retrieve, invalidate, and manage your own cache tags. Resources: Drupal 8 Cache API Big Pipe / Author zu CrewOn behalf of the team
/ Address 200-303 Pacific Ave S7K 1P2 Saskatoon, SK / Contact +1 306 653 4747 +1 800 834 8300 contactus@zu.com careers@zu.com / Social LinkedIn Instagram Facebook / Careers Be a part of a diverse team anddrive meaningful change. Join Us © zu.com communications, 1995-2026 Privacy Policy XCamp
智能索引记录
-
2026-03-01 00:40:27
教育培训
成功
标题:少年梦作文300字六篇
简介:在日常学习、工作和生活中,大家总少不了接触作文吧,作文根据写作时限的不同可以分为限时作文和非限时作文。还是对作文一筹莫展
-
2026-02-28 03:44:10
综合导航
成功
标题:胎位不正应该怎么办 - 云大夫
简介:如果胎位不正出现于在孕7个月之前,因为羊水相对较多,宝宝也比较小,此时不需要处理。在孕8个月以后,如果胎位还是不正,可以
-
2026-02-27 19:42:59
综合导航
成功
标题:术语表 XS
简介:查看我们的交易术语和定义词汇表
-
2026-02-28 03:34:30
综合导航
成功
标题:Anthroponomical. World English Historical Dictionary
简介:Anthroponomical. World English Historical Dictionary
-
2026-02-28 02:31:12
综合导航
成功
标题:Financial Model: 재무 모델 XS
简介:재무 모델은 회사의 재무 실적을 수학적으로 표현한 것으로, 미래의 결과를 예측하는 데 사용됩니다. 이러한 모
-
2026-03-01 02:16:20
教育培训
成功
标题:【热门】美丽的作文500字合集十篇
简介:在平凡的学习、工作、生活中,大家最不陌生的就是作文了吧,根据写作命题的特点,作文可以分为命题作文和非命题作文。相信写作文
-
2026-02-28 01:00:51
综合导航
成功
标题:Employee Benefit News HR & Benefits News and Insights Employee Benefit News
简介:Employee Benefit News offers expert HR management, healthcar
-
2026-03-01 05:31:46
教育培训
成功
标题:有你真好作文
简介:在生活、工作和学习中,大家都尝试过写作文吧,借助作文人们可以实现文化交流的目的。相信很多朋友都对写作文感到非常苦恼吧,以
-
2026-02-28 00:40:02
综合导航
成功
标题:MX Embedded Customer Digital & Mobile Experiences
简介:MX’s open finance APIs empower better money management and f
-
2026-03-01 05:08:47
教育培训
成功
标题:春的脚步作文600字五篇
简介:在日常学习、工作或生活中,大家都不可避免地要接触到作文吧,作文根据体裁的不同可以分为记叙文、说明文、应用文、议论文。那要
-
2026-03-01 00:27:41
图片素材
成功
标题:懂得的作文650字 描写懂得的作文 关于懂得的作文-作文网
简介:作文网精选关于懂得的650字作文,包含懂得的作文素材,关于懂得的作文题目,以懂得为话题的650字作文大全,作文网原创名师
-
2026-02-27 19:26:10
综合导航
成功
标题:ILSC Language Schools Programs Customized Group Packages French
简介:Let ILSC Language Schools organize French study & activity p
-
2026-02-28 03:03:55
教育培训
成功
标题:幽默大全:脱口秀台词.(经典搞笑) - 豆丁网
简介:豆丁网是面向全球的中文社会化阅读分享平台,拥有商业,教育,研究报告,行业资料,学术论文,认证考试,星座,心理学等数亿实用
-
2026-03-01 04:37:29
综合导航
成功
标题:Decision Making Archives - Making Sense of the Infinite
简介:Decision Making Archives - Making Sense of the Infinite
-
2026-02-28 03:21:49
综合导航
成功
标题:Bread-room. World English Historical Dictionary
简介:Bread-room. World English Historical Dictionary
-
2026-02-28 20:25:49
教育培训
成功
标题:五年级上册雨中即景作文[荐]
简介:在平平淡淡的学习、工作、生活中,大家对作文都再熟悉不过了吧,写作文可以锻炼我们的独处习惯,让自己的心静下来,思考自己未来
-
2026-02-28 02:43:01
综合导航
成功
标题:Yearbook Mono final selection complete
简介:1x.com is the world
-
2026-03-01 02:24:07
教育培训
成功
标题:二年级作文大全10篇
简介:在日常生活或是工作学习中,说到作文,大家肯定都不陌生吧,作文根据写作时限的不同可以分为限时作文和非限时作文。为了让您在写
-
2026-03-01 01:17:13
综合导航
成功
标题:关于月亮的作文500字
简介:小编:欢迎阅读与支持,如果喜欢记得常来!内容简介:天上的月亮像含羞的少女,躲在去层中,不肯出来。路灯在透明的灯罩中,洒下
-
2026-03-01 02:06:10
综合导航
成功
标题:JEK.com for sale Spaceship.com
简介:JEK.com is for sale on Spaceship. Secure checkout and quick
-
2026-03-01 05:28:02
图片素材
成功
标题:家乡的作文20字 描写家乡的作文 关于家乡的作文-作文网
简介:作文网精选关于家乡的20字作文,包含家乡的作文素材,关于家乡的作文题目,以家乡为话题的20字作文大全,作文网原创名师点评
-
2026-02-28 20:44:07
图片素材
成功
标题:早熟的作文100字 描写早熟的作文 关于早熟的作文-作文网
简介:作文网精选关于早熟的100字作文,包含早熟的作文素材,关于早熟的作文题目,以早熟为话题的100字作文大全,作文网原创名师
-
2026-03-01 00:52:03
综合导航
成功
标题:Schaeffler Germany
简介:Schaeffler has been driving forward groundbreaking invention
-
2026-03-01 00:28:42
电商商城
成功
标题:捷联科技 商城 - 电商网站
简介:捷联科技 多语言,多货币,多种国际配送方式,PayPal 支付,国际信用卡支付,PC Web 端和移动端自适应。
-
2026-03-01 05:17:02
综合导航
成功
标题:Google Chrome will finally stop slowing your PC down T3
简介:New and improved Google Chrome reduces the strain on your co
-
2026-03-01 00:45:28
综合导航
成功
标题:New Framework for telework between Germany and Austria
简介:Telework and hybrid work are present as never before. In the
-
2026-02-28 11:13:55
职场办公
成功
标题:客服工作台 - 全链路智能客服系统 - 阿里云
简介:客服工作台是阿里云提供的一站式全链路智能客服系统,整合机器人、人工与工单,支持在线与热线等多渠道统一响应,助您显著降低服
-
2026-03-01 05:19:47
教育培训
成功
标题:【推荐】成长的烦恼小学作文10篇
简介:在日常学习、工作和生活中,大家最不陌生的就是作文了吧,作文一定要做到主题集中,围绕同一主题作深入阐述,切忌东拉西扯,主题
-
2026-03-01 05:28:23
母婴亲子
成功
标题:天下最不能等待的事情莫过于孝敬父母-励志一生
简介:天下最不能等待的事情莫过于孝敬父母_天下最不能等待的事情莫过于孝敬父母 感恩父母,孝敬父母是我们中华民族的传统美德。在
-
2026-03-01 00:16:12
教育培训
成功
标题:成长风雨路作文
简介:在学习、工作或生活中,许多人都写过作文吧,根据写作命题的特点,作文可以分为命题作文和非命题作文。作文的注意事项有许多,你