---
title: Wagtail & Caching
class: text-center
highlighter: shiki
transition: slide-left
monaco: false
mdc: true
themeConfig:
primary: '#2e1f5e'
addons:
- "slidev-addon-qrcode"
---
# + Caching = ❤️
## Jake Howard{.mt-5}
### Wagtail Space NL 2024{.mt-5}
---
layout: full
---
# `$ whoami`
Made with Wagtail
Springload
```
---
layout: cover
background: /website-homepage.png
---
## Example:
# My website
---
layout: image
image: /website-posts.png
---
---
layout: none
---
````md magic-move
```html
{% load wagtailcore_tags util_tags %}
{% if breadcrumbs %}
{% include "common/breadcrumbs.html" with parents=page.get_parent_pages %}
{% endif %}
{% include "common/content-details.html" %}
{{ page.summary }}
```
```html {all|1|3,27|4-26|all}
{% load wagtailcore_tags wagtail_cache util_tags %}
{% wagtailpagecache FRAGMENT_CACHE_TTL "listing-item" breadcrumbs show_listing_images %}
{% if breadcrumbs %}
{% include "common/breadcrumbs.html" with parents=page.get_parent_pages %}
{% endif %}
{% include "common/content-details.html" %}
{{ page.summary }}
{% endwagtailpagecache %}
```
````
---
layout: cover
background: /website-search.png
---
# Duplication
---
layout: center
---
```html {3}
{% for page in listing_pages %}
{% include "common/listing-item.html" %}
{% endfor %}
```
---
layout: image
image: /website-search.png
---
---
# Cache invalidation
```python
>>> BlogPostPage.objects.first().cache_key
```
```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`.
"""
...
```
---
layout: center
---
# What about Django?
```jinja
{% load cache %}
{% cache 500 sidebar request.user.username %}
.. sidebar for logged in user ..
{% endcache %}
```
- Current page
- Current site
- Preview
- Cache invalidation
---
layout: section
---
## 2:
# Frontend Caching
---
layout: fact
---
```mermaid
flowchart LR
U[User]
CDN{Content
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
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
---
---
layout: cover
background: /wagtail-homepage.png
---
## Example
# `wagtail.org`
---
layout: image
image: /map.jpg
transition: fade
---
---
layout: cover
background: /map.jpg
---
# ~XXms
---
layout: fact
---
```mermaid
flowchart LR
U[User]
subgraph Content Delivery Network
CDN{Content
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
Delivery Network}
C[(Cache)]
end
W[(Wagtail)]
U---->CDN
CDN-.->C
C---->W
C-.->CDN
A---->CDN
CDN---->W
```
---
layout: section
---
# Content is cached!
```mermaid
flowchart LR
U1[User]
U2[User]
U3[User]
subgraph Content Delivery Network
CDN{Content
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
---
---
layout: section
---
## Solution:
# Frontend Cache Invalidation
### Wagtail secret sauce
---
layout: fact
---
```mermaid
flowchart LR
U[User]
subgraph Content Delivery Network
CDN{Content
Delivery Network}
C[(Cache)]
end
W[(Wagtail)]
U--->CDN
CDN-->C
C-->CDN
C--->W
W-.->|Frontend Cache Invalidation|CDN
```
---
layout: center
---
# `settings.py`
### 1.
```python {3}
INSTALLED_APPS = [
...
"wagtail.contrib.frontend_cache"
]
```
#### 2.
```python
WAGTAILFRONTENDCACHE = {
'cloudflare': {
'BACKEND': 'wagtail.contrib.frontend_cache.backends.CloudflareBackend',
'BEARER_TOKEN': os.environ["CF_BEARER_TOKEN"],
'ZONEID': os.environ["CF_ZONE_ID"],
},
}
```
#### 3. ???
#### 4. Profit!
---
layout: fact
---
```mermaid
flowchart LR
A[Admin]
subgraph Content Delivery Network
CDN{Content
Delivery Network}
C[(Cache)]
end
W[(Wagtail)]
A-->|Publish new content to
/blog/post-1
|W
W-.->|
PURGE /blog/post-1
|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:
- Users are happier
- Servers are happier
- The planet is happier
---
layout: end
---
END