Skip to content
Snippets Groups Projects
Verified Commit 939d0de2 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Cache generated HTML

parent d3321f06
No related branches found
No related tags found
1 merge request!1Resolve "Optimise query count"
from io import BytesIO
import hashlib
import json
import sys
from PIL import Image
from django.conf import settings
from django.core.cache import cache
from django.core.files.storage import default_storage as storage
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db import models
......@@ -26,6 +29,8 @@ config = getattr(settings, 'FAVICON_CONFIG', config)
if "shortcut icon" not in config or 32 not in config["shortcut icon"]:
config.setdefault("shortcut_icon", []).append(32)
config_cache_key = "favicons_" + hashlib.sha1(json.dumps(config, sort_keys=True).encode()).hexdigest()
image_path = getattr(settings, "FAVICON_PATH", "favicon")
......@@ -112,6 +117,8 @@ class Favicon(models.Model):
if self.faviconImage:
self.get_favicons(update=update)
cache.delete(config_cache_key)
class FaviconImg(models.Model):
faviconFK = models.ForeignKey(Favicon, on_delete=models.CASCADE)
......
from django import template
from django.core.cache import cache
from django.utils.safestring import mark_safe
from favicon.models import Favicon, config
from favicon.models import Favicon, config_cache_key
register = template.Library()
......@@ -16,6 +17,11 @@ def place_favicon(context):
{% place_favicon %}
"""
html = cache.get(config_cache_key)
if html:
return html
fav = Favicon.on_site.filter(isFavicon=True).first()
if not fav:
return mark_safe('<!-- no favicon -->')
......@@ -23,4 +29,5 @@ def place_favicon(context):
for n in fav.get_favicons():
html += f'<link rel="{n.rel}" sizes="{n.size}x{n.size}" href="{n.faviconImage.url}"/>'
cache.set(config_cache_key, html, timeout=None)
return mark_safe(html)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment