Skip to main content

Cache In Drupal

Drupal 8 core caching modules:

The Internal Page Cache module: this caches pages for anonymous users in the database. Pages requested by anonymous users are stored the first time they are requested and then are reused for future visitors.
The Internal Dynamic Page Cache module: This is a key feature that Drupal 7 did not have. Unlike the Internal Page module, the Dynamic Page module aims to speed up the site for both anonymous and logged-in users.

How Cache Works in Drupal:

There are two modules available in the drupal core
1 - Internal page cache: The Internal Page Cache module caches pages for anonymous users in the database. Pages requested by anonymous users are stored the first time they are requested and then are reused.
Configuring the internal page cache : 
 From the performance page we can set the maximum time how long browsers and proxies may cache pages based on the Cache-Control header.

2 - Internal dynamic page cache:
Drupal 8 provides the Dynamic Page Cache module that is recommended for websites of all sizes. It caches pages minus the personalized parts, and is therefore useful for all users (both anonymous & authenticated).

How to Disable dynamic page cache:

In example.settings.local.php, this section can be found:
# $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

Uncomment the line to disable the dynamic cache. By default, it is enabled.

Comparison to Drupal 7
1 - Drupal 7 has no instantaneous updates; the page cache in Drupal 8 is instantly updated when something has changed.
2 - Drupal 7 required the entire page cache to be cleared whenever any content was modified; Drupal 8 uses cache tags to only clear the cached pages that depend on the modified content.
3 - Drupal 7 kept serving outdated pages in many cases; any module (and even parts of Drupal 7 core) failed to clear the page cache.
Drupal 7's internal page cache is not enabled by default. Many users don’t know they should enable this. 
Drupal 8 enables page cache for anonymous users by default.
4- Drupal 7 did not have Dynamic Page Cache.

Default request cache Policy:
1- No CLI
2- No session open
3 - page cache kill switch 

\Drupal::service('page_cache_kill_switch')->trigger();
Calling the kill switch will stop both the page cache and the dynamic page cache from doing any caching on that page.

deafult respoce policy:

1- route has no no_cache option(no_cache:TRUE )
2- no server errors

Type Of Caching : 


1 - Opcode Caching (PHP - high CPU) : PHP out of the box is a dynamic language and can lead to heavy CPU usage on web servers. There are multiple types of opcode caching add-ons for PHP available that will convert your .php page into memory (byte code) to provide a major benefit in load time and reduced CPU usage.

2 - Database Caching : Database caching can be provided by a distributed memory object caching system, such as Memcache.
Memcached allows you to allocate memory where you have more than you need and make it accessible to areas where you have less than you need. When implemented with Drupal, Memcached can store the result of your database query’s in the memory for a specified time, reducing database traffic.

3 - Web Server (Proxy) Caching : HTTP acceleration, or Web Server (Proxy) Caching, will significantly reduce resource requirements and page load times. Varnish Cache, is a widely-used HTTP accelerator for Drupal sites.

4 - Authenticated Users : 
-shared server :
  Boost (static page caching for non-logged in visitors).
  Authcache ( For logedin users )
VPS or dedicated server :
  Boost (for example on a low RAM VPS)
  Memecache , Varnish (Both are used as a load balancer on multiple server) 

Internal page cache vs Internal dynamic Page cache : 

IPC If you have a e-commerce type site, then you can disable the internal page cache module, just becuase it is for ananymous users only.
dynamic cahce is new concept in d8, in d7 it's not there, based on dynamic caching we can cache the part of the pages like block,view,node user everything. bases on cache tags and cache context.

Tags : cache tags which can be used to invalidate a cache entry when something about your site changes, cache tags are like node:5,taxonomy_term:44,user:3 etc ..

Lets Suppose a node has all these tags When node is updated then it invalidate the cache and reload from the DB, But same if you update the term which is attached to story then the tags woud be invalidate everywhere ,  i.e node also so you no need to worry about the cache it will handle automatically if you use the cache tags properly.

Cache Context :A cache context is a string that refers to one of the available cache context services. like
theme,user,user.roles,url,url.query_args etc

Related Articles:

Comments

Popular posts from this blog

How to span column of custom table in Drupal

If you want to span the column of custom drupal table like below image, Follow the below code to make the header of the table , <?php $header = array('S.N','District', array('data' => '<div class ="house">Household </div><br><span>Rural</span> <p>Urban</p>','colspan' => 2), array('data' => '<div class ="house">Members</div> <br><span>Rural</span> <p>Urban</p>','colspan' => 2), 'Duplicate/Failed Registration', array('data' => '<div class ="house">Pending De duplication </div><br><span>Rural</span> <p>Urban</p>','colspan' => 2), 'Non Un-organised Workers', 'SSID Generated', 'No. of Card Personlised', ); $rows[] = arra...

Get The field values of node in Drupal 8

use Drupal \ node \ NodeInterface ; /** * Implements hook_ENTITY_TYPE_insert() for node entities. * * This tests saving a node on node insert. * * @see \Drupal\node\Tests\NodeSaveTest::testNodeSaveOnInsert() */ function node_test_node_insert ( NodeInterface $node ) { // Set the node title to the node ID and save. if ( $node - > getTitle ( ) == 'new' ) { $node - > setTitle ( 'Node ' . $node - > id ( ) ) ; $node - > setNewRevision ( FALSE ) ; $node - > save ( ) ; } } Now There is so many functions are there to get the values, For All the functions available visit the API code, https://api.drupal.org/api/drupal/core%21modules%21node%21src%21NodeInterface.php/interface/NodeInterface/8.2.x Some of as below, Node edit form, Drupal 8 Automatically Load the whole object no need to load the entity like below, if ($event->getFormId() == 'node_alexa_audio_clips_edit_form') { $node = \Drupal::ro...

Dependency Injection in Drupal 8

Here are the important nuggets: DI is a design pattern used in programming. DI uses composition. DI achieves inversion of control. Dependency == service that your class needs == object of a certain type. Inject == provide == compose == assemble. Container == service container == dependency container. Instead of using  \Drupal::service('foo_service') , get the service from the  $container  if using a class. And the important reasons: Externalizing dependencies makes code easier to test. It allows dependencies to be replaced without interfering with other functionality. Retrieving dependencies from the container is better for performance. Services: node.grant_storage The easiest examples to find are services that have arguments, because you can search *.services.yml files for the word "arguments". In  node.services.yml  for example, there is this entry: node.grant_storage: class: Drupal\node\NodeGrantDatabaseStorage argument...