Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/AccordBox/wagtail_cache_block
Quickly Add HTML fragment cache to your StreamField block
https://github.com/AccordBox/wagtail_cache_block
streamfield wagtail
Last synced: 2 months ago
JSON representation
Quickly Add HTML fragment cache to your StreamField block
- Host: GitHub
- URL: https://github.com/AccordBox/wagtail_cache_block
- Owner: AccordBox
- License: other
- Created: 2020-03-19T04:08:04.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-26T20:39:53.000Z (about 2 years ago)
- Last Synced: 2024-07-05T15:50:35.950Z (6 months ago)
- Topics: streamfield, wagtail
- Language: Python
- Homepage:
- Size: 40 KB
- Stars: 7
- Watchers: 1
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.rst
- Changelog: HISTORY.rst
- Contributing: CONTRIBUTING.rst
- License: LICENSE
Awesome Lists containing this project
- awesome-wagtail - Wagtail Cache Block - A templatetag which add HTML fragment cache to your StreamField block (Apps / StreamField)
README
.. image:: https://travis-ci.org/AccordBox/wagtail_cache_block.svg?branch=master
:target: https://travis-ci.org/AccordBox/wagtail_cache_block=============================
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``:
.. code-block:: python
INSTALLED_APPS = (
...
'wagtail_cache_block.apps.WagtailCacheBlockConfig',
...
)You can use it in StreamField ``for loop`` iteration
Here ``300`` is the cache timeout, ``request`` is Django ``RequestContext``, and ``block`` is the StreamField block.
Please remember to use ``{% include_block block with request=request block=block only %}`` to make sure ``request`` is available in the context.
.. code-block:: HTML
{% load wagtailcore_tags cache_block_tags %}
{% for block in page.body %}
{% cache_block 300 request block %}
{% include_block block with request=request block=block only %}
{% endcache_block %}
{% endfor %}Or you can use it in block template (For example: ``StructBlock``)
.. code-block:: HTML
{% load wagtailcore_tags cache_block_tags %}
{% cache_block 300 request block %}
{{ block.value.heading }}
{{ block.value.paragraph }}
{% endcache_block %}