Add page cache for learningpath pages
This commit is contained in:
parent
96020bf83d
commit
8edea0b92f
|
|
@ -25,7 +25,7 @@ export const useLearningPathStore = defineStore({
|
||||||
return this.learningPath;
|
return this.learningPath;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const learningPathData = await itGet(`/learnpath/api/learningpath/${slug}/`);
|
const learningPathData = await itGet(`/learnpath/api/page/${slug}/`);
|
||||||
const completionData = await itGet(`/api/completion/learning_path/${learningPathData.translation_key}/`);
|
const completionData = await itGet(`/api/completion/learning_path/${learningPathData.translation_key}/`);
|
||||||
|
|
||||||
this.learningPath = learningPathData;
|
this.learningPath = learningPathData;
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,7 @@ os.environ.setdefault("IT_APP_ENVIRONMENT", "development")
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.base")
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.base")
|
||||||
django.setup()
|
django.setup()
|
||||||
|
|
||||||
from vbv_lernwelt.learnpath.models import LearningPath
|
from wagtail.models import Page
|
||||||
from vbv_lernwelt.learnpath.serializers import LearningPathSerializer
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
@ -22,9 +21,9 @@ def main():
|
||||||
from django.db import reset_queries
|
from django.db import reset_queries
|
||||||
reset_queries()
|
reset_queries()
|
||||||
|
|
||||||
learning_path = LearningPath.objects.filter(slug='versicherungsvermittlerin', locale__language_code='de-CH').first()
|
page = Page.objects.get(slug='versicherungsvermittlerin', locale__language_code='de-CH')
|
||||||
|
serializer = page.specific.get_serializer_class()(page.specific)
|
||||||
|
|
||||||
serializer = LearningPathSerializer(learning_path)
|
|
||||||
print(serializer.data)
|
print(serializer.data)
|
||||||
print(len(json.dumps(serializer.data)))
|
print(len(json.dumps(serializer.data)))
|
||||||
print(len(connection.queries))
|
print(len(connection.queries))
|
||||||
|
|
|
||||||
|
|
@ -486,31 +486,28 @@ ALLOWED_HOSTS = env.list(
|
||||||
# CACHES
|
# CACHES
|
||||||
CACHES = {
|
CACHES = {
|
||||||
"default": {
|
"default": {
|
||||||
"BACKEND": env(
|
"BACKEND": env("IT_DJANGO_CACHE_BACKEND", default="django.core.cache.backends.db.DatabaseCache"),
|
||||||
"IT_DJANGO_CACHE_BACKEND",
|
|
||||||
default="django.core.cache.backends.db.DatabaseCache",
|
|
||||||
),
|
|
||||||
"LOCATION": env("IT_DJANGO_CACHE_LOCATION", default="django_cache_table"),
|
"LOCATION": env("IT_DJANGO_CACHE_LOCATION", default="django_cache_table"),
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if "django_redis.cache.RedisCache" in env("IT_DJANGO_CACHE_BACKEND", default=""):
|
if "django_redis.cache.RedisCache" in env("IT_DJANGO_CACHE_BACKEND", default=""):
|
||||||
CACHES = {
|
CACHES = {
|
||||||
"default": {
|
"default": {
|
||||||
"BACKEND": env(
|
"BACKEND": "django_redis.cache.RedisCache",
|
||||||
"IT_DJANGO_CACHE_BACKEND",
|
"LOCATION": env("IT_DJANGO_CACHE_LOCATION"),
|
||||||
default="django.core.cache.backends.db.DatabaseCache",
|
|
||||||
),
|
|
||||||
"LOCATION": env("IT_DJANGO_CACHE_LOCATION", default="django_cache_table"),
|
|
||||||
"OPTIONS": {
|
"OPTIONS": {
|
||||||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||||||
# Mimicing memcache behavior.
|
|
||||||
# https://github.com/jazzband/django-redis#memcached-exceptions-behavior
|
|
||||||
"IGNORE_EXCEPTIONS": True,
|
"IGNORE_EXCEPTIONS": True,
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CACHES["learning_path_cache"] = {
|
||||||
|
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
|
||||||
|
"LOCATION": "django_cache_learning_path",
|
||||||
|
}
|
||||||
|
|
||||||
# OAuth/OpenId Connect
|
# OAuth/OpenId Connect
|
||||||
|
|
||||||
OAUTH = {
|
OAUTH = {
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,12 @@ class LearningPath(Page):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.title}"
|
return f"{self.title}"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_serializer_class(cls):
|
||||||
|
return get_it_serializer_class(
|
||||||
|
cls, ['id', 'title', 'slug', 'type', 'translation_key', 'children']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Topic(Page):
|
class Topic(Page):
|
||||||
# title = models.TextField(default='')
|
# title = models.TextField(default='')
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,10 @@ class ItBaseSerializer(wagtail_serializers.BaseSerializer):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def get_children(self, obj):
|
def get_children(self, obj):
|
||||||
if self.descendants:
|
if not self.descendants:
|
||||||
children = _get_children(self.descendants, obj)
|
self.descendants = [p for p in obj.get_descendants().specific()]
|
||||||
return [c.specific.get_serializer_class()(c.specific, descendants=self.descendants).data for c in children]
|
children = _get_children(self.descendants, obj)
|
||||||
else:
|
return [c.specific.get_serializer_class()(c.specific, descendants=self.descendants).data for c in children]
|
||||||
return [c.specific.get_serializer_class()(c.specific).data for c in obj.get_children().specific()]
|
|
||||||
|
|
||||||
|
|
||||||
def _get_descendants(pages, obj):
|
def _get_descendants(pages, obj):
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
from rest_framework import serializers
|
|
||||||
|
|
||||||
from vbv_lernwelt.learnpath.models import Circle, LearningPath
|
|
||||||
from vbv_lernwelt.learnpath.serializer_helpers import get_it_serializer_class, _get_children
|
|
||||||
|
|
||||||
|
|
||||||
class LearningPathSerializer(get_it_serializer_class(LearningPath, [])):
|
|
||||||
children = serializers.SerializerMethodField()
|
|
||||||
|
|
||||||
meta_fields = []
|
|
||||||
|
|
||||||
def get_children(self, obj):
|
|
||||||
descendants = [p for p in obj.get_descendants().specific()]
|
|
||||||
return [c.specific.get_serializer_class()(c.specific, descendants=descendants).data for c in _get_children(descendants, obj)]
|
|
||||||
|
|
||||||
def get_meta_label(self, obj):
|
|
||||||
return obj._meta.label
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Circle
|
|
||||||
fields = ['id', 'title', 'slug', 'type', 'translation_key', 'children']
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
import structlog
|
||||||
|
from django.core.cache import caches
|
||||||
|
from django.db.models.signals import post_delete, post_save
|
||||||
|
from wagtail.models import Page
|
||||||
|
|
||||||
|
logger = structlog.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def invalidate_learning_path_cache(sender, **kwargs):
|
||||||
|
logger.debug('invalidate learning_path_cache', label='learning_path_cache')
|
||||||
|
caches['learning_path_cache'].clear()
|
||||||
|
|
||||||
|
|
||||||
|
for subclass in Page.__subclasses__():
|
||||||
|
post_save.connect(invalidate_learning_path_cache, subclass)
|
||||||
|
post_delete.connect(invalidate_learning_path_cache, subclass)
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
from django.urls import path, re_path
|
from django.urls import path, re_path
|
||||||
|
|
||||||
from .views import circle_view, generate_web_component_icons
|
from .views import generate_web_component_icons, page_api_view
|
||||||
from .views import learningpath_view
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path(r"api/circle/<slug:slug>/", circle_view, name="circle_view"),
|
path(r"api/page/<slug:slug>/", page_api_view, name="page_api_view"),
|
||||||
path(r"api/learningpath/<slug:slug>/", learningpath_view, name="learningpath_view"),
|
|
||||||
re_path(r"icons/$", generate_web_component_icons, name="generate_web_component_icons"),
|
re_path(r"icons/$", generate_web_component_icons, name="generate_web_component_icons"),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,19 @@ from pathlib import Path
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
from django.views.decorators.cache import cache_page
|
||||||
from rest_framework.decorators import api_view
|
from rest_framework.decorators import api_view
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from wagtail.models import Page
|
||||||
|
|
||||||
from vbv_lernwelt.core.middleware.auth import django_view_authentication_exempt
|
from vbv_lernwelt.core.middleware.auth import django_view_authentication_exempt
|
||||||
from vbv_lernwelt.learnpath.models import Circle, LearningPath
|
|
||||||
from vbv_lernwelt.learnpath.serializers import LearningPathSerializer
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def circle_view(request, slug):
|
@cache_page(60 * 60 * 8, cache="learning_path_cache")
|
||||||
circle = Circle.objects.get(slug=slug)
|
def page_api_view(request, slug):
|
||||||
serializer = Circle.get_serializer_class()(circle)
|
page = Page.objects.get(slug=slug, locale__language_code='de-CH')
|
||||||
|
serializer = page.specific.get_serializer_class()(page.specific)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -38,9 +39,3 @@ def generate_web_component_icons(request):
|
||||||
context={'svg_files': svg_files},
|
context={'svg_files': svg_files},
|
||||||
content_type="application/javascript"
|
content_type="application/javascript"
|
||||||
)
|
)
|
||||||
|
|
||||||
@api_view(['GET'])
|
|
||||||
def learningpath_view(request, slug):
|
|
||||||
learning_path = LearningPath.objects.get(slug=slug, locale__language_code='de-CH')
|
|
||||||
serializer = LearningPathSerializer(learning_path)
|
|
||||||
return Response(serializer.data)
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue