Improve markdown rendering performance
Reuse the markdown instance and cache data
This commit is contained in:
parent
b4b9ee3f2b
commit
aaf0bb4571
3 changed files with 22 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -174,3 +174,5 @@ poetry.toml
|
|||
pyrightconfig.json
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/python
|
||||
|
||||
cache/
|
||||
|
|
|
@ -3,6 +3,11 @@ from django.http import HttpResponse
|
|||
from django.db import connections
|
||||
from django.shortcuts import get_object_or_404
|
||||
import markdown
|
||||
from django.core.cache import caches
|
||||
from threading import Lock
|
||||
|
||||
md = markdown.Markdown()
|
||||
md_lock = Lock()
|
||||
|
||||
def search(request):
|
||||
with connections["default"].cursor() as cursor:
|
||||
|
@ -11,7 +16,14 @@ def search(request):
|
|||
pages = Page.objects.filter(id__in=row)
|
||||
return HttpResponse(str(pages), content_type="text/plain")
|
||||
|
||||
|
||||
def content(request, slug):
|
||||
page = get_object_or_404(Page, slug=slug)
|
||||
|
||||
return HttpResponse(markdown.markdown(page.content), content_type="text/html")
|
||||
if (cached_content := caches["default"].get(f"page-content-{slug}")) is None:
|
||||
with md_lock:
|
||||
cached_content = md.convert(page.content)
|
||||
md.reset()
|
||||
caches["default"].set(f"page-content-{slug}", cached_content)
|
||||
|
||||
return HttpResponse(cached_content, content_type="text/html")
|
||||
|
|
|
@ -84,6 +84,13 @@ YAMDL_DIRECTORIES = [
|
|||
|
||||
YAMDL_LOADER = "yamdl_playground.core.utils.CustomYamdlLoader"
|
||||
|
||||
CACHES = {
|
||||
"default": {
|
||||
"BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
|
||||
"LOCATION": BASE_DIR / "cache",
|
||||
}
|
||||
}
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
|
||||
|
||||
|
|
Loading…
Reference in a new issue