SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Drupal 8 Caching
Subhash Yadav
https://www.drupal.org/u/subhashuyadav
A cache is a stored copy of a resource so future requests for that
resource can be served faster.
The data stored in a cache might be the result of an earlier
computation, or the duplicate of data stored elsewhere.
Caches are found at every level of a content's journey from the
original server to the browser.
What is cache?
Why Caching?
● Faster than recomputing a result or reading from a slower
data store; thus the, more requests can be served from the
cache, the faster the system performs i.e. maximum output
with minimum latency.
● Eases the load of the server
● Improves responsiveness
HTTP Headers
HTTP Headers is where caching information is exchanged between the origin
server and cdn, proxies including varnish all the way to the browser.
Example : HTTP/1.x 200 OK
Transfer-Encoding: chunked
Date: Fri, 27 Nov 2017 04:36:25 GMT
Server: LiteSpeed
Connection: close
X-Powered-By: W3 Total Cache/0.8
Expires: Fri, 27 Nov 2017 05:36:25 GMT
Etag: "pub1259380237;gz"
Cache-Control: max-age=3600, public
Content-Type: text/html; charset=UTF-8
Last-Modified: Sat, 28 Nov 2009 03:50:37 GMT
Content-Encoding: gzip
Vary: Accept-Encoding, Cookie, User-Agent
The Expires header sets a time in the future
when the content will expire. This header is
probably best used only as a fall back.
Expires
A unique hash identifier of the cacheable object that the browser
caches.
Whenever a cached response is requested again, browser checks
with server if the eTag of the response is modified. If response is
not modified server returns 304 not modified else 200 along
with new response & new eTag.
Etag
The date-time this cacheable object was last cached.
(Similar to eTag)
Last-modified
When a cache receives a request that can be satisfied by a
cached response that has a Vary header field, it must not use
that cached response unless all header fields as nominated
by the Vary header match in both the original (cached)
request and the new request.
This can be useful for serving content dynamically.
Vary
Each resource can define its caching policy via the
Cache-Control HTTP header.
Cache-Control directives control who can cache the
response, under which conditions, and for how long.
Cache-Control
no-cache: Indicates that the returned response can't be used to satisfy a
subsequent request to the same URL without first checking with the
server if the response has changed.
no-store: It simply disallows the browser and all intermediate caches
from
storing any version of the returned response.
public / private: If the response is marked as "public", then it can be cached, even
if
it has HTTP authentication associated with it, and even when the
response status code isn't normally cacheable.
private marked responses are typically intended for
a single user, so
an intermediate cache is not allowed to cache them but browser can
cache them.
Cache-Control Directives
max-age : Configures the maximum age that the content may be cached
before
it must revalidate or re-download the content from the origin server.
This replaces the Expires header for modern browsing.
This option takes its value in seconds with a maximum valid
freshness time of one year (31536000 seconds).
s-maxage : This is very similar to the max-age setting, in that it indicates the
amount of time that the content can be cached.
The difference is that this option is applied only to intermediary
caches.
Cache-Control Directives
All things that either are directly renderable or are used to determine
what to render provide cacheability metadata.
Cacheability metadata consists of three properties:
cache tags : For dependencies on data managed by
Drupal, like
entities & configuration.
cache contexts : For variations, i.e. dependencies on the request
context.
cache max-age : For time-sensitive caching, i.e. time dependencies
Cacheability metadata
Cache tags provide a declarative way to track which cache items
depend on some data managed by Drupal.
The role of the tags is to identify cache items across multiple bins for
proper invalidation.
In a typical scenario, a user may have modified a node that appears in
two views, three blocks, and on twelve pages. Without cache tags, we
couldn't know which cache items to invalidate. So we'd have to
invalidate everything and sacrifice effectiveness to achieve
correctness. With cache tags we can have both.
Cache Tags
Syntax: “Thing:identifier”
Example: node:5 -- cache tag for node entity 5
user:4 -- cache tag for user entity 4
node_list -- list cache tag for node
$tags = array('node:1', 'user:7');
Drupal::cache()->set($cid, $data,
CacheBackendInterface::CACHE_PERMANENT, $tags);
// Invalidate all cache items with certain tags.
DrupalCoreCacheCache::invalidateTags($tags);
Cache Tags
● Cache contexts provide a declarative way to create context-
dependent variations of something that needs to be cached.
● Determines how to vary items according to request.
● Similar to D7 block constants like DRUPAL_NO_CACHE /
DRUPAL_CACHE_PER_ROLE / DRUPAL_CACHE_PER_PAGE,
but with many more options.
● Cache contexts are hierarchical in nature.
Cache Contexts
Example : theme -- vary by negotiated theme.
user.roles -- vary by the combination of
roles
$variables['#cache']['contexts'][] = 'url.path';
Cache Contexts
Cache max-age
● Cache max-age provides a declarative way to create time-
dependent caches.
● Controls how long an item may be cached by number of seconds.
● 0 means cacheable for zero seconds, i.e. not cacheable
● DrupalCoreCacheCache::PERMANENT means cacheable
forever, i.e. this will only ever be invalidated due to cache tags.
(In other words: ∞, or infinite seconds)
● Varnish is just a web-server in front of origin webserver.
● Also known as a caching HTTP reverse proxy.
● It can be used with cache tags to make cache invalidation easy.
● Every request to the origin web-server goes through varnish.
● If varnish doesn’t have a request cached, the request is passed to
the backend. Upon response, varnish reads the response
headers and if response is cacheable varnish caches the
response for next similar requests.
Varnish cache
● Varnish will only cache resources that are requested through an
idempotent HTTP verb, which are the verbs that do not change
the state of the resources.
● HTTP verbs that Varnish can handle: GET, HEAD
● PUT, POST, DELETE, TRACE, OPTIONS cannot be cached by
the varnish.
● Varnish default cache setting is 120 sec (2 min).
Varnish cache
You need to do three things to make sure Varnish works well with the
cache tags generated by Drupal:
● Update your Varnish VCL so it handles BAN requests properly.
Location : /etc/default/varnish/default.vcl
http://foshttpcache.readthedocs.io/en/stable/varnish-
configuration.html#tagging
Varnish cache
Varnish cache
● Send a cache tags header:
Drupal's Purge module automatically configures
Purge-Cache-Tags headers.
● Send a BAN request when content or configuration changes:
Add an HTTP Purger using Generic HTTP Purger module.
Configure a cron job to process the Purge queue.
Thank you

Weitere ähnliche Inhalte

Was ist angesagt?

Php & web server performace
Php & web server performacePhp & web server performace
Php & web server performace
Tuyển Đoàn
 
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sites
drupalcampest
 
Drupal caching
Drupal cachingDrupal caching
Drupal caching
Exove
 
Performance Optimization using Caching | Swatantra Kumar
Performance Optimization using Caching | Swatantra KumarPerformance Optimization using Caching | Swatantra Kumar
Performance Optimization using Caching | Swatantra Kumar
Swatantra Kumar
 

Was ist angesagt? (20)

Presentation1
Presentation1Presentation1
Presentation1
 
RESTful Web services in Drupal 8
RESTful Web services in Drupal 8RESTful Web services in Drupal 8
RESTful Web services in Drupal 8
 
Php & web server performace
Php & web server performacePhp & web server performace
Php & web server performace
 
Understanding Web Cache
Understanding Web CacheUnderstanding Web Cache
Understanding Web Cache
 
World Wide Web Caching
World Wide Web CachingWorld Wide Web Caching
World Wide Web Caching
 
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sites
 
Drupal caching
Drupal cachingDrupal caching
Drupal caching
 
Performance Optimization using Caching | Swatantra Kumar
Performance Optimization using Caching | Swatantra KumarPerformance Optimization using Caching | Swatantra Kumar
Performance Optimization using Caching | Swatantra Kumar
 
Caching Strategies
Caching StrategiesCaching Strategies
Caching Strategies
 
23 Ways To Speed Up WordPress
23 Ways To Speed Up WordPress23 Ways To Speed Up WordPress
23 Ways To Speed Up WordPress
 
Sofia WP User Group Presentation
Sofia WP User Group PresentationSofia WP User Group Presentation
Sofia WP User Group Presentation
 
Optimize
OptimizeOptimize
Optimize
 
Insight on MongoDB Change Stream - Abhishek.D, Mydbops Team
Insight on MongoDB Change Stream - Abhishek.D, Mydbops TeamInsight on MongoDB Change Stream - Abhishek.D, Mydbops Team
Insight on MongoDB Change Stream - Abhishek.D, Mydbops Team
 
Mini-Training: To cache or not to cache
Mini-Training: To cache or not to cacheMini-Training: To cache or not to cache
Mini-Training: To cache or not to cache
 
Memcached: What is it and what does it do? (PHP Version)
Memcached: What is it and what does it do? (PHP Version)Memcached: What is it and what does it do? (PHP Version)
Memcached: What is it and what does it do? (PHP Version)
 
Memcached: What is it and what does it do?
Memcached: What is it and what does it do?Memcached: What is it and what does it do?
Memcached: What is it and what does it do?
 
Артем Сильчук - Respond in 60ms. Extremal optimization with reinventing a wheel
Артем Сильчук - Respond in 60ms. Extremal optimization with reinventing a wheelАртем Сильчук - Respond in 60ms. Extremal optimization with reinventing a wheel
Артем Сильчук - Respond in 60ms. Extremal optimization with reinventing a wheel
 
Caching with Ruby
Caching with RubyCaching with Ruby
Caching with Ruby
 
Cloud Hosting Services
Cloud Hosting ServicesCloud Hosting Services
Cloud Hosting Services
 
Postgres connections at scale
Postgres connections at scalePostgres connections at scale
Postgres connections at scale
 

Ähnlich wie Caching in Drupal 8

Web Site Optimization
Web Site OptimizationWeb Site Optimization
Web Site Optimization
Sunil Patil
 
Web site optimization
Web site optimizationWeb site optimization
Web site optimization
Sunil Patil
 

Ähnlich wie Caching in Drupal 8 (20)

The Most Frequently Used Caching Headers
The Most Frequently Used Caching HeadersThe Most Frequently Used Caching Headers
The Most Frequently Used Caching Headers
 
cache concepts and varnish-cache
cache concepts and varnish-cachecache concepts and varnish-cache
cache concepts and varnish-cache
 
[Hanoi-August 13] Tech Talk on Caching Solutions
[Hanoi-August 13] Tech Talk on Caching Solutions[Hanoi-August 13] Tech Talk on Caching Solutions
[Hanoi-August 13] Tech Talk on Caching Solutions
 
Varnish –Http Accelerator
Varnish –Http AcceleratorVarnish –Http Accelerator
Varnish –Http Accelerator
 
Content Caching with NGINX and NGINX Plus
Content Caching with NGINX and NGINX PlusContent Caching with NGINX and NGINX Plus
Content Caching with NGINX and NGINX Plus
 
Browser Caching
Browser CachingBrowser Caching
Browser Caching
 
Ror caching
Ror cachingRor caching
Ror caching
 
Basic Caching Terminology
Basic Caching TerminologyBasic Caching Terminology
Basic Caching Terminology
 
Caching on the web
Caching on the webCaching on the web
Caching on the web
 
Http caching
Http cachingHttp caching
Http caching
 
Http Caching for the Android Aficionado
Http Caching for the Android AficionadoHttp Caching for the Android Aficionado
Http Caching for the Android Aficionado
 
Web Site Optimization
Web Site OptimizationWeb Site Optimization
Web Site Optimization
 
Web site optimization
Web site optimizationWeb site optimization
Web site optimization
 
Caching
CachingCaching
Caching
 
Nginx dhruba mandal
Nginx dhruba mandalNginx dhruba mandal
Nginx dhruba mandal
 
Caching in drupal
Caching in drupalCaching in drupal
Caching in drupal
 
Academy PRO: HTML5 Data storage
Academy PRO: HTML5 Data storageAcademy PRO: HTML5 Data storage
Academy PRO: HTML5 Data storage
 
Nginx
NginxNginx
Nginx
 
Cdn technology overview
Cdn technology overviewCdn technology overview
Cdn technology overview
 
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sites
 

Mehr von valuebound

How to Use DDEV to Streamline Your Drupal Development Process.
How to Use DDEV to Streamline Your Drupal Development Process.How to Use DDEV to Streamline Your Drupal Development Process.
How to Use DDEV to Streamline Your Drupal Development Process.
valuebound
 
How to Use AWS to Automate Your IT Operation| Valuebound
How to Use AWS to Automate Your IT Operation| Valuebound How to Use AWS to Automate Your IT Operation| Valuebound
How to Use AWS to Automate Your IT Operation| Valuebound
valuebound
 
The Benefits of Cloud Engineering
The Benefits of Cloud EngineeringThe Benefits of Cloud Engineering
The Benefits of Cloud Engineering
valuebound
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
valuebound
 
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
valuebound
 

Mehr von valuebound (20)

Scaling Drupal for High Traffic Websites
Scaling Drupal for High Traffic WebsitesScaling Drupal for High Traffic Websites
Scaling Drupal for High Traffic Websites
 
Drupal 7 to Drupal 10 Migration A Fintech Strategic Blueprint (1).pdf
Drupal 7 to Drupal 10 Migration A Fintech Strategic Blueprint (1).pdfDrupal 7 to Drupal 10 Migration A Fintech Strategic Blueprint (1).pdf
Drupal 7 to Drupal 10 Migration A Fintech Strategic Blueprint (1).pdf
 
How to Use DDEV to Streamline Your Drupal Development Process.
How to Use DDEV to Streamline Your Drupal Development Process.How to Use DDEV to Streamline Your Drupal Development Process.
How to Use DDEV to Streamline Your Drupal Development Process.
 
How to Use AWS to Automate Your IT Operation| Valuebound
How to Use AWS to Automate Your IT Operation| Valuebound How to Use AWS to Automate Your IT Operation| Valuebound
How to Use AWS to Automate Your IT Operation| Valuebound
 
How to Use Firebase to Send Push Notifications to React Native and Node.js Apps
How to Use Firebase to Send Push Notifications to React Native and Node.js AppsHow to Use Firebase to Send Push Notifications to React Native and Node.js Apps
How to Use Firebase to Send Push Notifications to React Native and Node.js Apps
 
Mastering Drupal Theming
Mastering Drupal ThemingMastering Drupal Theming
Mastering Drupal Theming
 
The Benefits of Cloud Engineering
The Benefits of Cloud EngineeringThe Benefits of Cloud Engineering
The Benefits of Cloud Engineering
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
 
Deep dive into ChatGPT
Deep dive into ChatGPTDeep dive into ChatGPT
Deep dive into ChatGPT
 
Content Creation Solution | Valuebound
Content Creation Solution | ValueboundContent Creation Solution | Valuebound
Content Creation Solution | Valuebound
 
Road ahead for Drupal 8 contributed projects
Road ahead for Drupal 8 contributed projectsRoad ahead for Drupal 8 contributed projects
Road ahead for Drupal 8 contributed projects
 
Chatbot with RASA | Valuebound
Chatbot with RASA | ValueboundChatbot with RASA | Valuebound
Chatbot with RASA | Valuebound
 
Drupal and Artificial Intelligence for Personalization
Drupal and Artificial Intelligence for Personalization Drupal and Artificial Intelligence for Personalization
Drupal and Artificial Intelligence for Personalization
 
Drupal growth in last year | Valuebound
Drupal growth in last year | ValueboundDrupal growth in last year | Valuebound
Drupal growth in last year | Valuebound
 
BE NEW TO THE WORLD "BRAVE FROM CHROME"
BE NEW TO THE WORLD "BRAVE FROM CHROME"BE NEW TO THE WORLD "BRAVE FROM CHROME"
BE NEW TO THE WORLD "BRAVE FROM CHROME"
 
Event loop in browser
Event loop in browserEvent loop in browser
Event loop in browser
 
The Basics of MongoDB
The Basics of MongoDBThe Basics of MongoDB
The Basics of MongoDB
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
Dependency Injection in Drupal 8
Dependency Injection in Drupal 8Dependency Injection in Drupal 8
Dependency Injection in Drupal 8
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 

Caching in Drupal 8

  • 1. Drupal 8 Caching Subhash Yadav https://www.drupal.org/u/subhashuyadav
  • 2. A cache is a stored copy of a resource so future requests for that resource can be served faster. The data stored in a cache might be the result of an earlier computation, or the duplicate of data stored elsewhere. Caches are found at every level of a content's journey from the original server to the browser. What is cache?
  • 3. Why Caching? ● Faster than recomputing a result or reading from a slower data store; thus the, more requests can be served from the cache, the faster the system performs i.e. maximum output with minimum latency. ● Eases the load of the server ● Improves responsiveness
  • 4. HTTP Headers HTTP Headers is where caching information is exchanged between the origin server and cdn, proxies including varnish all the way to the browser. Example : HTTP/1.x 200 OK Transfer-Encoding: chunked Date: Fri, 27 Nov 2017 04:36:25 GMT Server: LiteSpeed Connection: close X-Powered-By: W3 Total Cache/0.8 Expires: Fri, 27 Nov 2017 05:36:25 GMT Etag: "pub1259380237;gz" Cache-Control: max-age=3600, public Content-Type: text/html; charset=UTF-8 Last-Modified: Sat, 28 Nov 2009 03:50:37 GMT Content-Encoding: gzip Vary: Accept-Encoding, Cookie, User-Agent
  • 5. The Expires header sets a time in the future when the content will expire. This header is probably best used only as a fall back. Expires
  • 6. A unique hash identifier of the cacheable object that the browser caches. Whenever a cached response is requested again, browser checks with server if the eTag of the response is modified. If response is not modified server returns 304 not modified else 200 along with new response & new eTag. Etag
  • 7. The date-time this cacheable object was last cached. (Similar to eTag) Last-modified
  • 8. When a cache receives a request that can be satisfied by a cached response that has a Vary header field, it must not use that cached response unless all header fields as nominated by the Vary header match in both the original (cached) request and the new request. This can be useful for serving content dynamically. Vary
  • 9. Each resource can define its caching policy via the Cache-Control HTTP header. Cache-Control directives control who can cache the response, under which conditions, and for how long. Cache-Control
  • 10. no-cache: Indicates that the returned response can't be used to satisfy a subsequent request to the same URL without first checking with the server if the response has changed. no-store: It simply disallows the browser and all intermediate caches from storing any version of the returned response. public / private: If the response is marked as "public", then it can be cached, even if it has HTTP authentication associated with it, and even when the response status code isn't normally cacheable. private marked responses are typically intended for a single user, so an intermediate cache is not allowed to cache them but browser can cache them. Cache-Control Directives
  • 11. max-age : Configures the maximum age that the content may be cached before it must revalidate or re-download the content from the origin server. This replaces the Expires header for modern browsing. This option takes its value in seconds with a maximum valid freshness time of one year (31536000 seconds). s-maxage : This is very similar to the max-age setting, in that it indicates the amount of time that the content can be cached. The difference is that this option is applied only to intermediary caches. Cache-Control Directives
  • 12. All things that either are directly renderable or are used to determine what to render provide cacheability metadata. Cacheability metadata consists of three properties: cache tags : For dependencies on data managed by Drupal, like entities & configuration. cache contexts : For variations, i.e. dependencies on the request context. cache max-age : For time-sensitive caching, i.e. time dependencies Cacheability metadata
  • 13. Cache tags provide a declarative way to track which cache items depend on some data managed by Drupal. The role of the tags is to identify cache items across multiple bins for proper invalidation. In a typical scenario, a user may have modified a node that appears in two views, three blocks, and on twelve pages. Without cache tags, we couldn't know which cache items to invalidate. So we'd have to invalidate everything and sacrifice effectiveness to achieve correctness. With cache tags we can have both. Cache Tags
  • 14. Syntax: “Thing:identifier” Example: node:5 -- cache tag for node entity 5 user:4 -- cache tag for user entity 4 node_list -- list cache tag for node $tags = array('node:1', 'user:7'); Drupal::cache()->set($cid, $data, CacheBackendInterface::CACHE_PERMANENT, $tags); // Invalidate all cache items with certain tags. DrupalCoreCacheCache::invalidateTags($tags); Cache Tags
  • 15. ● Cache contexts provide a declarative way to create context- dependent variations of something that needs to be cached. ● Determines how to vary items according to request. ● Similar to D7 block constants like DRUPAL_NO_CACHE / DRUPAL_CACHE_PER_ROLE / DRUPAL_CACHE_PER_PAGE, but with many more options. ● Cache contexts are hierarchical in nature. Cache Contexts
  • 16. Example : theme -- vary by negotiated theme. user.roles -- vary by the combination of roles $variables['#cache']['contexts'][] = 'url.path'; Cache Contexts
  • 17. Cache max-age ● Cache max-age provides a declarative way to create time- dependent caches. ● Controls how long an item may be cached by number of seconds. ● 0 means cacheable for zero seconds, i.e. not cacheable ● DrupalCoreCacheCache::PERMANENT means cacheable forever, i.e. this will only ever be invalidated due to cache tags. (In other words: ∞, or infinite seconds)
  • 18. ● Varnish is just a web-server in front of origin webserver. ● Also known as a caching HTTP reverse proxy. ● It can be used with cache tags to make cache invalidation easy. ● Every request to the origin web-server goes through varnish. ● If varnish doesn’t have a request cached, the request is passed to the backend. Upon response, varnish reads the response headers and if response is cacheable varnish caches the response for next similar requests. Varnish cache
  • 19. ● Varnish will only cache resources that are requested through an idempotent HTTP verb, which are the verbs that do not change the state of the resources. ● HTTP verbs that Varnish can handle: GET, HEAD ● PUT, POST, DELETE, TRACE, OPTIONS cannot be cached by the varnish. ● Varnish default cache setting is 120 sec (2 min). Varnish cache
  • 20. You need to do three things to make sure Varnish works well with the cache tags generated by Drupal: ● Update your Varnish VCL so it handles BAN requests properly. Location : /etc/default/varnish/default.vcl http://foshttpcache.readthedocs.io/en/stable/varnish- configuration.html#tagging Varnish cache
  • 21. Varnish cache ● Send a cache tags header: Drupal's Purge module automatically configures Purge-Cache-Tags headers. ● Send a BAN request when content or configuration changes: Add an HTTP Purger using Generic HTTP Purger module. Configure a cron job to process the Purge queue.