Skip to main content

Easily make your Wagtail StreamBlock cacheable

Project description

https://travis-ci.org/AccordBox/wagtail_cache_block.svg?branch=master

wagtail_cache_block

This project add HTML fragment cache to your StreamField block in easy way

Background

In Wagtail community, it is popular to use the HTML fragment cache at page level.

This works fine in most of the time. But what if some different page have the same block data and some DB query need be run multiple times when rendering?

In this case, it make sense to make HTML fragment cache work on block level instead of page level.

How it works

  1. cache_block is very similar with Django {% cache %} templatetag, it would pull data from block automatically and use the value to generate fragment cache key.

  2. If value in any block field (even descendants of the block) has changed, new fragment key would be generated and new HTML fragment code would be saved to Cache.

  3. cache_block would check if the page is preview mode, if the page if in preview mode, the HTML fragment cache would not be pulled from cache.

I have used it in my projects and the performance has been improved, especially for some page which contains many db query.

Quickstart

Install wagtail_cache_block:

pip install wagtail_cache_block

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'wagtail_cache_block.apps.WagtailCacheBlockConfig',
    ...
)

You can use it in StreamField for loop iteration

Here 300 is the cache timeout, page is Wagtail page instance, and block is the StreamField block.

{% load wagtailcore_tags cache_block_tags %}

{% for block in page.body %}
  {% cache_block 300 page block %}
    {% include_block block %}
  {% endcache_block %}
{% endfor %}

Or you can use it in block template (For example: StructBlock)

{% load wagtailcore_tags cache_block_tags %}

{% cache_block 300 page block %}
  <section class="{{ block.block_type }}">

    {{ block.value.heading }}
    {{ block.value.paragraph }}

  </section>
{% endcache_block %}

History

0.1.0 (2020-01-19)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail_cache_block-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wagtail_cache_block-0.1.1-py2.py3-none-any.whl (6.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file wagtail_cache_block-0.1.1.tar.gz.

File metadata

  • Download URL: wagtail_cache_block-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.8

File hashes

Hashes for wagtail_cache_block-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b848d48baa8c225cdf9aa594497a7329fe6e2c4b3187a280db21a73cf10bc526
MD5 68591b6e2d7539c0df46abd98647f080
BLAKE2b-256 3ba80585fe66551fb6fb4e37e12398f14cba33dee89332eb56ab7ff9e24d9315

See more details on using hashes here.

File details

Details for the file wagtail_cache_block-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: wagtail_cache_block-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.8

File hashes

Hashes for wagtail_cache_block-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 deb9cd65f8a976ee84b4587bdf0ed19191f5b1a61080148524034755e64a7781
MD5 741abc3a8b234e032244330186b101e8
BLAKE2b-256 65c391f7c2464fd37746f6b50406d652ba6518b8823c9b7de46724aa0798c8cb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page