-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1072 lines (986 loc) · 41.9 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Drupal Developer Days Lisbon 2018: Drupal and cache</title>
<link rel="shortcut icon" href="images/DDD2018/favicon.ico" type="image/vnd.microsoft.icon" />
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/DDD2018.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal ddd2018">
<div class="slides">
<!-- ################## FIRST SLIDE ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_Intro_Belem_tower.png"
data-background-color="#4d73ae"
>
<h1 class="mainTitle">Drupal and Cache</h1>
<h2 class="mainSubTitle">Olivier Briat </h2>
<a href="https://twitter.com/@Eiffel_Alec">@Eiffel_Alec</a>
</section>
<!-- ################## Quote SLIDE ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">THANKS TO THE SPONSORS</h1>
</section>
<!-- ################## Diamond Sponsor SLIDE ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h1>Diamond Sponsor</h1>
<img alt="" src="images/DDD2018/sponsors-diamond.png">
</section>
<!-- ################## Platinum Sponsor SLIDE ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h1>Platinum Sponsor</h1>
<img alt="" src="images/DDD2018/sponsors-platinium.png">
</section>
<!-- ################## Gold Sponsor SLIDE ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h1>Gold Sponsor</h1>
<img alt="" src="images/DDD2018/sponsors-gold.png">
</section>
<!-- ################## 1 ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h1>Olivier Briat</h1>
<div class="columns">
<div class="col3">
<img alt="Mugface" src="img/me-myself-and-I.png"><br>
<img alt="Capgemini logo" src="img/Capgemini.png">
</div>
<ul class="col">
<li>Capgemini Nantes (France)</li>
<li>Drupal lead developer</li>
<li>Work on <a href="https://www.sncf.com">www.sncf.com</a>, <a href="https://oui.sncf">oui.sncf</a>, <a href="https://www.operadeparis.fr">www.operadeparis.fr</a></li>
<li>5 years on Drupal (6, 7, now 8)</li>
<li>I added the sentinel mode for the <a href="https://www.drupal.org/project/redis">Drupal 8 Redis module</a></li>
</ul>
</div>
<aside class="notes">
C'est moi
</aside>
</section>
<!-- ################## 2 ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h1>Inspirations</h1>
<p>Proudly found elsewhere</p>
<ul>
<li><b>Drupal 8 Caching overview</b> / Berdir / Drupal Dev Days Seville 2017</li>
<li><b>Render API & Cache API</b> / Artusamak / Drupal Camp Nantes 2016</li>
<li><b>Drupal 8 Caching - A Developer’s Guide</b> / Peter Sawczynec / Pacific Northwest Drupal Submit 2016
</li>
</ul>
<aside class="notes">
</aside>
</section>
<!-- ################## 4 ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h1>Topics</h1>
<ul>
<li>Caches upstream of Drupal
<ul>
<li>Browser</li>
<li>Network caches</li>
</ul>
</li>
<li>Internal Page Cache (aka "Anonymous cache")</li>
<li>Dynamic Page Caching</li>
<li>Render API cache system</li>
<li>Using Drupal cache mechanisms in your custom code</li>
<li>Cache storage</li>
<li>Questions ?</li>
</ul>
<aside class="notes">
</aside>
</section>
<!-- ################## Quote SLIDE ################## -->
<section
class="section-slide"
>
<section
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">What is cache?</h1>
<p class="fragment">Store the result of slow calculations so that they do not need to be executed again and so improve performances</p>
</section>
</section>
<!-- ################## Cache mecanisms upstream of Drupal ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">Cache mecanisms upstream of Drupal</h1>
</section>
<!-- ################## Browser 1/3 ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>The first cache is the browser one</h2>
<div class="columns">
<div >
<img alt="" src="img/header.png">
</div>
<ul>
<li>The url is the browser cache key</li>
<li>The browser will only check for updates after <code style="color:#45dd01">max-age</code> seconds or at <code style="color:#ed6500">expires</code></li>
<li>The checksum used is usualy <code style="color:#45dd01">ETag</code> or <code style="color:#ed6500">Last-Modified</code></li>
<li>It will get a new content or a <code>304</code> response</li>
<li><code style="color:#0c53b1">Vary</code> indicates alternate version of cache content : encoding, cookies, ...</li>
<li>We'll see later the <code style="color:#ffea00">X-Drupal</code> headers</li>
</ul>
</div>
</section>
<!-- ################## Browser 2/3 ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Drupal settings</h2>
<div class="columns">
<div>
<img alt="" src="img/Drupal8-cache-header-settings.png">
</div>
<ul>
<li><code>max-age</code> is set in the admin pages</li>
<li>There's only one setting for all webpages </li>
<li>Modules that give more controls:
<ul>
<li><a href="https://www.drupal.org/project/http_cache_control">HTTP Cache Control</a></li>
<li><a href="https://www.drupal.org/project/http_response_headers">HTTP response headers</a> modules offer fine grain control</li>
</ul>
</li>
</ul>
</div>
<aside class="notes">
</aside>
</section>
<!-- ################## Browser 3/3 ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Assets cache settings</h2>
<ul>
<li>For files other than a Response object, <code>max-age</code> could be tune in .htaccess</li>
</ul>
<pre><code class="apache" data-trim
># Requires mod_expires to be enabled.
<IfModule mod_expires.c>
# Enable expirations.
ExpiresActive On
# Cache all files for 2 weeks after access (A).
ExpiresDefault A1209600
<FilesMatch \.php$ >
# Do not allow PHP scripts to be cached unless they explicitly
# send cache headers themselves(...)
ExpiresActive Off
</FilesMatch>
</IfModule></code></pre>
<aside class="notes">
</aside>
</section>
<!-- ################## Cache mecanisms upstream of Drupal ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">Before going any further</h1>
</section>
<!-- ################## Disable cache ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Unlike Drupal 7, cache is enable by default in Drupal8, so...</h2>
<div class="fragment left">
<p>it is strongly recommended to disable it when coding</p>
<p>You just have to uncomment the following lines in <code>settings.php</code></p>
<pre><code class="php"
>#Copy sites/example.settings.local.php to sites/default/settings.local.php
if (file_exists(__DIR__ . '/settings.local.php')) {
include __DIR__ . '/settings.local.php';
}</code></pre>
<p>or use drupal console: <code>drupal site:mode dev</code> <small>@see https://www.drupal.org/node/2598914</small> </p>
</div>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<code>settings.local.php</code>
<pre><code class="php" style="width:115%"
># Load development services
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
# Display verbose errors
$config['system.logging']['error_level'] = 'verbose';
# Do not minify css & js
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
# Disable render caching
# $settings['cache']['bins']['render'] = 'cache.backend.null';
# Disable dynamic page caching
# $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';</code></pre>
<code>development.services.yml</code>
<pre><code class="php" style="width:115%"
>parameters:
# Enable cache debug headers
http.response.debug_cacheability_headers: true
services:
# Enable a "null" cache storage
cache.backend.null:
class: Drupal\Core\Cache\NullBackendFactory
</code></pre>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>But, must of all</h2>
<h1 class="fragment">Don't forget to re-enable cache before testing your code!</h1>
</section>
<!-- ################## Cache mecanisms upstream of Drupal ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">Network caches</h1>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h3>On their way to Drupal, requests could pass through:</h3>
<ul>
<li>Proxies: which respect <code>max-age</code> & <code>s-max-age</code> headers</li>
<li>Content Delivery Network (CDN) : Akamaï, Cloudflare, ...
<li>Varnish:
<ul>
<li>Drupal best buddy</li>
<li>Works like a proxy (default cache ttl is 2mn)</li>
<li>Swiss Army knife for HTTP headers and requests</li>
<li>Could serve your website even if it's down (grace mode)</li>
<li>Could replace Drupal <code>Internal Page Cache</code></li>
</ul>
</li>
<li>Must of them could use <code>X-Drupal-Cache-Tags</code> headers</li>
<li>They also can be managed with the <a href="https://www.drupal.org/project/purge">purge</a>
module and its sub-modules (varnish_purge, akamai, ...) and the
<a href="https://www.drupal.org/project/http_cache_control">HTTP Cache Control</a> module</li>
</ul>
<aside class="notes">
s is for "shared" ()
</aside>
</section>
<!-- ################## Internal Page Cache ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">Internal Page Cache</h1>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Anonymous cache</h2>
<ul>
<li>Managed by the <u>Internal Page Cache</u> module</li>
<li>It caches fully rendered HTML responses</li>
<li>Only for <b>anonymous</b> visitors (no session)</li>
<li>Really fast</li>
<li>It also uses URL as the cache key</li>
<li>It's based only on the <code>cache tags</code> metadata send by Drupal</li>
<li>It respects the <code>Expires</code> header on the response Object</li>
<li>As seen before, it could be disabled if an external page cache system is used (like Varnish)</li>
<li>Debugging HTTP Header: <code>X-Drupal-Cache: HIT #or MISS</code></li>
</ul>
</section>
<!-- ################## Dynamic Page Caching ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">Dynamic Page Caching</h1>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<section>
<h2>Dynamic Page Cache</h2>
<ul>
<li>Managed by the <u>Dynamic Page Cache</u> module</li>
<li>Much slower than Anonymous cache</li>
<li>Works for anonymous and authenticate users!</li>
<li>Is invalidated by cache metadata that come from page contents</li>
<li>It does not cache page contents that are too dynamic, they are handle by "Lazyloading"</li>
</ul>
</section>
<aside class="notes">
</aside>
</section>
<!-- ################## Render API cache system ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">Render API cache system</h1>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Render API cache system</h2>
<ul>
<li>The page content are built with the render API, it's where most of the cache meta-data come from</li>
<li>They are stored in the <code>#cache</code> key of render arrays:
<ul>
<li><code>keys</code>:
<span style="font-size:0.8em">Name this cache part and marks it as cacheable, e.g.: <code>['node', 5, 'teaser']</code></span>
</li>
<li><code>max-age</code>*:
<ul>
<li style="font-size:0.8em">-1: Permanent</li>
<li style="font-size:0.8em">X: Age in seconds</li>
<li style="font-size:0.8em">0: Do not cache</li>
</ul>
</li>
<li><code>context</code>:
<span style="font-size:0.8em">Allows cache variations according to: theme, language, user roles, permissions, URL, QS, timezone,...</span>
</li>
<li><code>tags</code>:
<span style="font-size:0.8em">Allows to invalidate all caches tagged with them: node:x, config:, user:x, library_info, route_match, node_list, ...</span>
</li>
</ul>
</li>
<p></p>
<small class="left">* This will not change the <code>max-age</code> HTTP header or the anonymous cache duration, see:
<a href="https://www.drupal.org/node/2352009">https://www.drupal.org/node/2352009</a> and
<a href="https://www.drupal.org/project/cache_control_override">https://www.drupal.org/project/cache_control_override</a>
</small>
</ul>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Bubbling</h2>
<p>They bubble from bottom to top arrays </p>
<div class="fragment" style="text-align: center;width:50%;float:left;">
<img alt="" src="img/render_array_cache.png" height="250vh"><br>Child element render array
</div>
<div class="fragment" style="text-align: center;width:50%;float:left;">
<img alt="" src="img/render_array_cache2.png" height="250vh"><br>Parent element render array
</div>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Bubbling</h2>
<div class="columns">
<ul>
<li>These cache metadata aggregate to form the whole page cache metadata</li>
<li>As seen before they are used to invalidate <u>Dynamic Page Cache</u></li>
<li><u>Internal Page Cache</u> is only invalidated by tags</li>
<li>External caches like Varnish could use <code style="color:#ffea00">X-Drupal-Cache-Tags</code> HTTP headers</li>
<li>These headers are also useful for debugging</li>
</ul>
<div>
<img alt="" src="img/header.png">
</div>
</div>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p style="font-size: 0.9em">Here's a page with two blocks containing each a nodes list <small>(cache tags are displayed between brackets, notice the bubbling)</small></p>
<img alt="" src="img/invalidation_example_start.svg" height="500">
<br>
<small>Diagram by @berdir</small>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p class="medium">Let's modify node n°5 and create a new sport node (n°7).<br> These actions will invalidate cache tags <span class="red">(red)</span> </p>
<img alt="" src="img/invalidation_example_invalidated.svg" height="500">
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p class="medium">Here in <span class="red">red</span> all the content parts that should be rebuild, in green the ones that will come from Render caching </p>
<img alt="" src="img/invalidation_example_rebuild_3.svg" height="500">
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Code example: setting cache metadata on a <code>Render Array</code></p>
<pre><code class="php" style="max-height:80vh;width:65vw"
>$config = \Drupal::config('system.site');
$current_user = \Drupal::currentUser();
$build = [
'#markup' => t('Hi, %name, welcome on @site!', [
'%name' => $current_user->getUsername(),
'@site' => $config->get('name'),
]),
'#cache' => [
'contexts' => [
'user', // So this block will be processed by #lazybuilding
],
'tags' => $config->getCacheTags(), // Same as 'tags' => ['config:system.site']
],
];
// Another way to add cache depency.
$renderer = \Drupal::service('renderer');
$renderer->addCacheableDependency(
$build,
\Drupal\user\Entity\User::load($current_user->id())
);
// Merge cache tags
$tags = Cache::mergeTags($conf_one->getCacheTags(),$conf_two->getCacheTags());
</code></pre>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Objects that render content (like plugins) should implement
the <code>CacheableDependencyInterface</code> and its methods:</p>
<pre><code class="php" style="max-height:80vh;width:60vw"
>\Drupal\Core\Cache
public function getCacheContexts() {
return Cache::mergeContexts(
parent::getCacheContexts(),
['user.node_grants:view']
);
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), ['node_list']);
}
public function getCacheMaxAge() {
return 0;
}</code></pre>
<small>About blocks, they have to render content through twig template or these method will not be taken into account:<br>
<a href="https://www.previousnext.com.au/blog/ensuring-drupal-8-block-cache-tags-bubble-up-page">https://www.previousnext.com.au/blog/ensuring-drupal-8-block-cache-tags-bubble-up-page</a>
</small>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Plugins could also inherit cache by using <i>annotation context</i><p>
<p>Here this block will inherit the node cache metadata</p>
<pre><code class="php" style="max-height:80vh;width:60vw"
>* context = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* }
...
$this->getContextValue('node');
</code></pre>
<p class="left">See:<br>
<small>
<a href="https://drupal.stackexchange.com/questions/199527/how-do-i-correctly-setup-caching-for-my-custom-block-showing-content-depending-o">https://drupal.stackexchange.com/questions/199527/how-do-i-correctly-setup-caching-for-my-custom-block-showing-content-depending-o</a><br>
<a href="https://api.drupal.org/api/drupal/core!core.api.php/group/annotation/8.5.x">https://api.drupal.org/api/drupal/core!core.api.php/group/annotation/8.5.x</a><br>
<a href="https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Annotation!ContextDefinition.php/group/plugin_context/8.5.x">https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Annotation!ContextDefinition.php/group/plugin_context/8.5.x</a>
</small>
</p>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>#lazy_builder (Auto-placeholdering)</h2>
<ul>
<li>To prevent highly dynamics content to systematically invalid page cache,
<u>Dynamic Page Cache</u> replaces them with placeholders before writing cache.</li>
<li>Each time the cache is requested their content will be rendered on the fly.</li>
<li>This mechanism is also used by <u>Bigpipe</u> (now enable by default in 8.5)</li>
</ul>
<p>
<a style="font-size: 1.5rem" href="https://www.drupal.org/docs/8/api/render-api/auto-placeholdering">https://www.drupal.org/docs/8/api/render-api/auto-placeholdering</a><br>
<a style="font-size: 1.5rem" href=" BigPipe : https://youtu.be/JwzX0Qv6u3A">Bigpipe demo</a>
</p>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p class="left">Lazybuilder triggering:<p>
<ul>
<li>Could be set manually with a <code>#lazy_builder</code> key:
<pre><code class="php"
>// Callback : class:method or (better) service:method
return [
'#lazy_builder' => ['hello_world.lazy_builder:renderSalutation', []],
'#create_placeholder' => TRUE,
];</code></pre>
</li>
<li>be automatically detected by Drupal, following the rules defined into <code class="medium">core/core.services.yml</code>:
<pre><code class="yaml"
>renderer.config:
auto_placeholder_conditions:
max-age: 0
contexts: ['session', 'user']
tags: []</code></pre>
</li>
<li>you could change them or add your own ones.</li>
</ul>
</section>
<!-- ################## Render API cache system ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h2>Drupal already handles all these cache mechanisms, so "everything is cool"</h2>
<h2 class="fragment">But you will certainly need to cache your own custom data, so here's the cache API</h2>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Static variables</h2>
<ul>
<li>Simple "caching" method using the fact that php static variables aren't destroyed at the end of a function execution</li>
<li><code>drupal_static()</code> could still be used for procedural code</li>
<li>As for OO, don't forget that "services" are "singletons" and therefore their properties are persistents</li>
</ul>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Drupal cache Getter / Setter</p>
<pre><code class="php"
>function my_custom_data_processing();
// Define my cache key
$key = 'my_module' . ':' . __FUNCTION__;
// Does the cache exists ?
if ($cache = \Drupal::cache()->get($key)) {
$data = $cache->data;
}
// No cache: process the data and set the cache.
else {
$data = my_slow_data_process();
\Drupal::cache()->set($key, $data);
}
return $data;
}</code></pre>
<p>Multiples</p>
<pre><code class="php"
>\Drupal::cache()->getMultiple(['key1','key2',...]);
\Drupal::cache()->setMultiple(['key1' => ['data' => 1],'key2' =>...]);</code></pre>
</section>
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Delete cache</p>
<pre><code class="php"
>// Deleting cache (fast)
\Drupal::cache()->delete('my_module:cached_data');
\Drupal::cache()->deleteMultiple([
'my_module:key1',
'my_module:key2',
...
]);
\Drupal::cache()->deleteAll();;</code></pre>
<p>Set an expiration</p>
<pre><code class="php"
>// Add a cache lifetime to my cache (timestamp)
\Drupal::cache()->set($key, $data, REQUEST_TIME + 600);
</code></pre>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Add tags</p>
<pre><code class="php"
>// Add tags to my cache
\Drupal::cache()->set(
$key,
$data,
Cache::PERMANENT,
[
'tag1',
'node:1',
'config:system.menu',
'config:my_module',
]
);</code></pre>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Get tags</p>
<pre><code class="php"
>// Get tags from an object that implements CacheableDependencyInterface:
$node->getCacheTags();
\Drupal\views\Entity\View::load('front')->getCacheTags();
// Get "List" cache tags associated with this entity type
// Will detects newly created entities
$em = \Drupal::entityTypeManager();
//config:taxonomy_vocabulary_list
$em->getDefinition('taxonomy_vocabulary')->getListCacheTags();
// taxonomy_term_list
$em->getDefinition('taxonomy_term')->getListCacheTags();
</code></pre>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Invalidate tags</p>
<pre><code class="php"
>// Should implements CacheTagsInvalidatorInterface
// my_tag invalidation counter = 0
$cache_tag_invalidator->invalidateTags(['my_tag']);
// my_tag invalidation counter = 1
Cache::invalidateTags(['my_tag']);
// my_tag invalidation counter =2</code></pre>
<p style="font-size: 0.8em">On cache writing, all tags invalidation counter are sum up to provide the cache checksum.</p>
<p style="font-size: 0.8em">On cache reading, the checksum is computed again, if it differs, the cache is rebuilt.</p>
<aside class="notes">
Chaque tag se voit attribuer un compteur qui est incrémenté à chacune de ses invalidations. Le checksum du contenu est simplement la somme de ces compteurs.<br>
</aside>
</section>
<!-- ################## ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>Return stale cache while building the new one</p>
<pre><code class="php"
>$cache = \Drupal::cache()->get('my-key', TRUE);
if ($cache && $cache->valid) {
return $cache->data;
} elseif (\Drupal::lock()->acquire('my-key')) {
// Lock the cache and rebuild it.
$data = my_slow_data_process();
\Drupal::cache()->set('my-key', $data);
return $data;
} elseif ($cache) {
// Someone else is rebuilding, work with stale data.
return $cache->data;
} else {
// Wait or rebuild.
}</code></pre>
</section>
<!-- ################## Cache Storages ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">Cache Storages</h1>
</section>
<!-- ################## Bins ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Cache Bins</h2>
<p class="left">Cache is split into containers:</p>
<ul>
<li><b>default</b>: Default, for small-ish, few key variations </li>
<li>data : Bigger caches, many key variations</li>
<li>discovery: Small, frequently used, usually for plugins and similar discovery processes</li>
<li>bootstrap: Drupal bootstraping</li>
<li><b>render</b>: HTML rendering cache</li>
<li>config: Used for caching configuration</li>
<li>static: Memory only, when persistence is not desired </li>
</ul>
<p class="left">Each could be linked to a specific storage</p>
</section>
<!-- ################## Bins ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<p>You could define your own bin as a service</p>
<pre><code class="yml"
>#yourmodule.services.yml
cache.your_bin:
class: Drupal\Core\Cache\CacheBackendInterface
tags:
- { name: cache.bin }
factory: cache_factory:get
arguments: [your_bin]
</code></pre>
<p>and enable it in <code>settings.php</code></p>
<pre><code class="php"
>// Use redis by default.
$settings['cache']['default'] = 'cache.backend.redis';
// Use the null backend to disable caching for certain bins.
$settings['cache']['bins']['render'] = 'cache.backend.null';</code></pre>
<aside class="notes">
bootstrap est rarement invalidé.<br>
Oui d'ailleurs c'est quoi un backend ?
</aside>
</section>
<!-- ################## Backends ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_white_bg.png"
>
<h2>Cache Backends</h2>
<p class="left">Where cache are actually stored</p>
<ul>
<li class="fragment">Core backends <code class="medium">(core/core.services.yml)</code>:
<ul class="medium">
<li>Memory: not persisted across requests</li>
<li><b>Database</b>: Default storage in the (SQL) database <b>(invalidated, never deleted*)</b></li>
<li><b>APCu</b>: Shared-Memory inside the PHP process, not shared with drush/CLI/multiple servers</li>
<li>Null: for disabling cache (dev)</li>
</ul>
</li>
<li class="fragment">Contributed backends:
<ul class="medium">
<li>Slushi Cache: A database backend with a configurable max lifetime </li>
<li>Memcache : RAM object database server <code>cache.backend.memcache_storage</code></li>
<li>Redis: RAM Key/value database server <code>cache.backend.redis</code></li>
</ul>
</li>
</ul>
<p class="left fragment medium">ChainedFast Backend (<code>cache.backend.chainedfast</code>) allow to "chained" a fast backend on top a slow but shared one.
</p>
<p class="left fragment medium"><a href="https://www.drupal.org/node/2891281">* : Since 8.4 SQL bins have a max size.</a></p>
<aside class="notes">
</aside>
</section>
<!-- ################## Quote SLIDE ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"
>
<h1 class="sectionTitle">Questions / Feedback?</h1>
<h2>@Eiffel_Alec</h2>
</section>
<!-- ################## References ################## -->
<section
class="section-slide"
data-background-transition="fade"
data-transition="fade"
data-background="images/DDD2018/DDD2018_quote_bg.png"
data-background-color="#4d73ae"