1
Fork 0
wagtail-caching/slides.md

661 lines
10 KiB
Markdown
Raw Normal View History

2024-04-19 11:38:03 +01:00
---
title: Wagtail & Caching
class: text-center
highlighter: shiki
transition: slide-left
2024-05-03 13:33:41 +01:00
monaco: false
2024-04-19 11:38:03 +01:00
mdc: true
themeConfig:
primary: '#2e1f5e'
---
2024-06-09 15:01:51 +01:00
# <logos-wagtail class="fill-white" /> + Caching = ❤️
2024-04-19 11:38:03 +01:00
2024-06-09 15:01:51 +01:00
## Jake Howard{.mt-5}
2024-04-19 11:38:03 +01:00
2024-06-09 15:01:51 +01:00
### Wagtail Space NL 2024{.mt-5}
<!--
- Here to talk about Caching with Wagtail
- Dramatically improve the performance of your site
- Sometimes, for very little effort
-->
---
layout: full
---
# `$ whoami`
<ul class="list-none! [&>li]:m-0! text-2xl mt-10">
<li><mdi-fire class="fill-white"/> Senior Systems Engineer @ Torchbox</li>
<li><logos-wagtail class="fill-white"/> Core, Security & Performance teams @ Wagtail</li>
<li><mdi-server-plus class="fill-white" /> I'm an avid self-hoster</li>
<li><mdi-robot-excited class="fill-white" /> I help students build robots in my "spare" time <small>(https://srobo.org)</small></li>
2024-04-19 11:38:03 +01:00
</ul>
2024-06-09 15:01:51 +01:00
<ul class="list-none! text-sm [&>li]:m-0! mt-10 text-xl">
2024-04-19 11:38:03 +01:00
<li><mdi-earth /> theorangeone.net</li>
<li><mdi-github /> @RealOrangeOne</li>
2024-05-07 23:18:13 +01:00
<li><mdi-twitter /> @RealOrangeOne</li>
2024-04-19 11:38:03 +01:00
<li><mdi-mastodon /> @jake@theorangeone.net</li>
</ul>
2024-04-19 17:46:32 +01:00
2024-06-09 15:01:51 +01:00
<!--
- Hi
- I'm Jake
- Senior Systems Engineer at Torchbox
- I'm also on the security team, performance team, and as of 2 weeks ago the core team for Wagtail
- I exist in many places on the internet
-->
<style>
.slidev-layout {
background-color: #e85537;
}
</style>
2024-05-07 23:18:13 +01:00
2024-04-19 17:46:32 +01:00
---
layout: fact
---
2024-06-10 21:35:09 +01:00
# Disclaimer{style="color: #fd5765;"}
2024-04-19 17:46:32 +01:00
---
2024-06-09 15:01:51 +01:00
layout: section
2024-04-19 17:46:32 +01:00
---
# What _is_ caching?
---
layout: center
---
2024-05-03 13:33:41 +01:00
# Scale
<style>
@keyframes grow {
from {
2024-06-10 21:35:09 +01:00
font-size: 0.5rem;
2024-05-03 13:33:41 +01:00
}
to {
font-size: 30rem;
}
}
h1 {
animation-name: grow;
animation-duration: 30s;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
</style>
2024-04-19 17:46:32 +01:00
---
2024-06-09 15:01:51 +01:00
layout: section
2024-04-19 17:46:32 +01:00
---
2024-05-03 13:33:41 +01:00
# <logos-wagtail class="fill-white"/> Caching <em>in Wagtail</em>
---
layout: section
---
## 1:
# Template Fragment Caching
---
layout: image
image: https://d1nvwtjgmbo5v5.cloudfront.net/media/images/Screen_Shot_2015-05-14_at_09.01.5.2e16d0ba.fill-1200x996.png
backgroundSize: cover
---
<style>
.slidev-layout {
background-position: top center !important;
}
</style>
---
layout: none
---
```html
<div class="one-half--medium one-third--large">
<div class="card card--link">
<a href="/developers/springload/made-wagtail/" class="project-image">
<img src="https://d1nvwtjgmbo5v5.cloudfront.net/media/images/Screen_Shot_2015-05-14_at_09.01.52.2e16d0ba.fill-680x564.png" alt="" title="">
</a>
<div class="project__links">
<a class="project__info" href="/developers/springload/made-wagtail/">
<span class="u-sr-only">Project info</span>
<svg class="i i--grey i--hover">
<use xlink:href="#i-info"></use>
</svg>
</a>
<a class="project__visit" href="http://madewithwagtail.org/" title="Project link" data-analytics="Project|Link click">
<span class="u-sr-only">Project link</span>
<svg class="i i--grey i--hover">
<use xlink:href="#i-visit"></use>
</svg>
</a>
</div>
</div>
<h4 class="project-title">
Made with Wagtail
</h4>
<p class="project-author">
<a href="/developers/springload/" title="View the company page of Springload">
Springload
</a>
</p>
```
<style>
.slidev-page, .slidev-code-wrapper, .slidev-code {
height: 100%;
}
</style>
2024-04-19 17:46:32 +01:00
---
layout: cover
2024-05-03 13:33:41 +01:00
background: /website-homepage.png
2024-04-19 17:46:32 +01:00
---
2024-06-09 15:01:51 +01:00
## Example:
# My website
2024-06-10 21:35:09 +01:00
### https://theorangeone.net
2024-05-03 13:33:41 +01:00
---
layout: image
image: /website-posts.png
---
---
layout: none
---
````md magic-move
```html
{% load wagtailcore_tags util_tags %}
<article class="media listing-item">
<div class="columns">
<figure class="media-left column is-{{ show_listing_images|yesno:'3,1' }} image-column">
{% if page.list_image_url %}
<a href="{% pageurl page %}" class="image" title="{{ page.title }}">
<img src="{{ page.list_image_url }}" alt="{{ page.hero_image_alt }}" loading="lazy" decoding="async" referrerpolicy="no-referrer" />
</a>
{% endif %}
</figure>
<div class="media-content column">
<div>
{% if breadcrumbs %}
{% include "common/breadcrumbs.html" with parents=page.get_parent_pages %}
{% endif %}
<h2 class="title is-3">
<a href="{% pageurl page %}">{{ page.title }}</a>
</h2>
{% include "common/content-details.html" %}
<p>{{ page.summary }}</p>
</div>
</div>
</div>
</article>
```
2024-06-10 21:35:09 +01:00
```html {all|3,27|4-26|all}
{% load wagtailcore_tags wagtail_cache %}
2024-05-03 13:33:41 +01:00
{% wagtailpagecache FRAGMENT_CACHE_TTL "listing-item" breadcrumbs show_listing_images %}
<article class="media listing-item">
<div class="columns">
<figure class="media-left column is-{{ show_listing_images|yesno:'3,1' }} image-column">
{% if page.list_image_url %}
<a href="{% pageurl page %}" class="image" title="{{ page.title }}">
<img src="{{ page.list_image_url }}" alt="{{ page.hero_image_alt }}" loading="lazy" decoding="async" referrerpolicy="no-referrer" />
</a>
{% endif %}
</figure>
<div class="media-content column">
<div>
{% if breadcrumbs %}
{% include "common/breadcrumbs.html" with parents=page.get_parent_pages %}
{% endif %}
<h2 class="title is-3">
<a href="{% pageurl page %}">{{ page.title }}</a>
</h2>
{% include "common/content-details.html" %}
<p>{{ page.summary }}</p>
</div>
</div>
</div>
</article>
{% endwagtailpagecache %}
```
````
<style>
.slidev-page, .slidev-code-wrapper, .slidev-code {
height: 100%;
}
</style>
---
layout: cover
background: /website-search.png
2024-06-10 21:35:09 +01:00
class: text-right
2024-05-03 13:33:41 +01:00
---
2024-06-10 21:35:09 +01:00
# Search
2024-05-03 13:33:41 +01:00
---
layout: center
---
```html {3}
<section class="container">
{% for page in listing_pages %}
{% include "common/listing-item.html" %}
{% endfor %}
</section>
```
<style>
code {
font-size: 1.5rem !important;
}
</style>
---
layout: image
image: /website-search.png
---
---
2024-06-10 21:35:09 +01:00
# Updating cached content
2024-05-03 13:33:41 +01:00
<v-click>
```python
>>> BlogPostPage.objects.first().cache_key
```
</v-click>
2024-06-09 15:01:51 +01:00
<br />
2024-05-03 13:33:41 +01:00
<v-click>
```python
@property
def cache_key(self) -> str:
"""
A generic cache key to identify a page in its current state.
Should the page change, so will the key.
Customizations to the cache key should be made in :attr:`get_cache_key_components`.
"""
...
```
</v-click>
<style>
code {
font-size: 1rem !important;
}
</style>
---
layout: center
---
# What about Django?
2024-06-10 21:35:09 +01:00
````md magic-move
2024-05-03 13:33:41 +01:00
```jinja
{% load cache %}
{% cache 500 sidebar request.user.username %}
.. sidebar for logged in user ..
{% endcache %}
```
2024-06-10 21:35:09 +01:00
```jinja
{% load wagtail_cache %}
{% wagtailcache 500 sidebar request.user.username %}
.. sidebar for logged in user ..
{% endwagtailcache %}
```
````
<v-click>
2024-05-03 13:33:41 +01:00
- Current page
- Current site
2024-06-10 21:35:09 +01:00
- Skip in preview
2024-05-03 13:33:41 +01:00
2024-06-10 21:35:09 +01:00
</v-click>
2024-05-03 13:33:41 +01:00
---
layout: section
---
## 2:
# Frontend Caching
2024-05-03 17:47:51 +01:00
---
layout: fact
---
```mermaid
flowchart LR
U[User]
CDN{Content<br>Delivery Network}
W[(Wagtail)]
U--->CDN
CDN-..->W
```
---
layout: fact
---
```mermaid
flowchart TD
U1[🧑‍💻]
U2[🧑‍💻]
U3[🧑‍💻]
U4[🧑‍💻]
U5[🧑‍💻]
U6[🧑‍💻]
U7[🧑‍💻]
U8[🧑‍💻]
U9[🧑‍💻]
U10[🧑‍💻]
U11[🧑‍💻]
U12[🧑‍💻]
U13[🧑‍💻]
CDN{Content<br>Delivery Network}
W[(Wagtail)]
U1--->CDN
U2--->CDN
U3--->CDN
U4--->CDN
U5--->CDN
U6--->CDN
U7--->CDN
U8--->CDN
U9--->CDN
U10--->CDN
U11--->CDN
U12--->CDN
U13--->CDN
CDN-...->W
```
---
layout: image
image: https://www.cloudflare.com/network-maps/cloudflare-pops-2O04nulSdNrRpJR9fs9OKv.svg
2024-06-10 21:35:09 +01:00
class: bg-white
2024-05-03 17:47:51 +01:00
---
---
layout: cover
background: /wagtail-homepage.png
---
2024-06-09 15:01:51 +01:00
## Example
2024-06-10 21:35:09 +01:00
# https://wagtail.org
2024-06-09 15:01:51 +01:00
<style>
code {
background-color: transparent !important;
}
</style>
2024-05-03 17:47:51 +01:00
---
layout: image
image: /map.jpg
transition: fade
---
---
layout: cover
background: /map.jpg
---
# ~XXms
<style>
h1 {
font-size: 4rem;
}
</style>
---
layout: fact
---
```mermaid
flowchart LR
U[User]
subgraph Content Delivery Network
CDN{Content<br>Delivery Network}
C[(Cache)]
end
W[(Wagtail)]
U---->CDN
CDN-.->C
C---->W
C-.->CDN
```
---
layout: fact
---
```mermaid
flowchart LR
U[User]
A[Admin]
subgraph Content Delivery Network
CDN{Content<br>Delivery Network}
C[(Cache)]
end
W[(Wagtail)]
U---->CDN
CDN-.->C
C---->W
C-.->CDN
A---->CDN
CDN---->W
```
---
2024-06-09 15:01:51 +01:00
layout: section
2024-05-03 17:47:51 +01:00
---
# Content is cached!
```mermaid
flowchart LR
U1[User]
U2[User]
U3[User]
subgraph Content Delivery Network
CDN{Content<br>Delivery Network}
C[(Cache)]
end
W[(Wagtail)]
U1===>CDN
U2===>CDN
U3===>CDN
CDN==>C
C==>CDN
C-.....->W
```
---
layout: image
image: /wagtail-homepage-typo.png
2024-06-10 21:35:09 +01:00
transition: fade
---
---
layout: cover
background: /wagtail-homepage-typo.png
2024-05-03 17:47:51 +01:00
---
2024-06-10 21:35:09 +01:00
# It doesn't*
2024-05-03 17:47:51 +01:00
---
layout: section
---
## Solution:
# Frontend Cache Invalidation
### Wagtail secret sauce <logos-wagtail class="fill-white"/>
---
layout: fact
---
```mermaid
flowchart LR
U[User]
subgraph Content Delivery Network
CDN{Content<br>Delivery Network}
C[(Cache)]
end
W[(Wagtail)]
U--->CDN
CDN-->C
C-->CDN
2024-06-09 15:01:51 +01:00
C--->W
2024-05-03 17:47:51 +01:00
W-.->|Frontend Cache Invalidation|CDN
```
---
layout: center
---
# `settings.py`
2024-06-10 21:35:09 +01:00
<v-click>
2024-05-03 17:47:51 +01:00
### 1.
```python {3}
INSTALLED_APPS = [
...
"wagtail.contrib.frontend_cache"
]
```
2024-06-10 21:35:09 +01:00
</v-click>
<v-click>
2024-05-03 17:47:51 +01:00
#### 2.
```python
WAGTAILFRONTENDCACHE = {
'cloudflare': {
'BACKEND': 'wagtail.contrib.frontend_cache.backends.CloudflareBackend',
'BEARER_TOKEN': os.environ["CF_BEARER_TOKEN"],
'ZONEID': os.environ["CF_ZONE_ID"],
},
}
```
2024-06-10 21:35:09 +01:00
</v-click>
<v-click>
2024-05-03 17:47:51 +01:00
#### 3. ???
#### 4. Profit!
2024-06-10 21:35:09 +01:00
</v-click>
2024-05-03 17:47:51 +01:00
<style>
code {
font-size: 0.9rem !important;
}
</style>
---
layout: fact
---
```mermaid
flowchart LR
A[Admin]
subgraph Content Delivery Network
CDN{Content<br>Delivery Network}
C[(Cache)]
end
W[(Wagtail)]
2024-06-10 21:35:09 +01:00
A-->|Publish new content to<br>blog post 1|W
2024-05-03 17:47:51 +01:00
W-.->|<code>PURGE /blog/post-1</code>|CDN
CDN-.->C
```
---
layout: cover
background: https://d1nvwtjgmbo5v5.cloudfront.net/media/images/Screen_Shot_2015-05-14_at_09.01.5.2e16d0ba.fill-1200x996.png
---
# Conclusion
---
layout: cover
---
# Performance means:
2024-06-10 21:35:09 +01:00
<Transform :scale="1.5">
<v-clicks>
2024-05-03 17:47:51 +01:00
- Users are happier <mdi-emoticon-happy />
- Servers are happier <mdi-server-network />
- The planet is happier <mdi-earth class="text-green" />
2024-06-10 21:35:09 +01:00
</v-clicks>
2024-05-03 17:47:51 +01:00
</Transform>
---
layout: end
---
2024-05-07 23:18:13 +01:00
END