basic json caching

This commit is contained in:
Ross
2020-11-23 16:21:56 +00:00
parent f345ca4447
commit 0ff1c39ffd
5 changed files with 57 additions and 8 deletions
@@ -0,0 +1,25 @@
# Generated by Django 3.1.2 on 2020-11-23 16:19
import anatomy.models
import django.core.files.storage
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('anatomy', '0003_auto_20201119_1941'),
]
operations = [
migrations.AddField(
model_name='exam',
name='recreate_json',
field=models.BooleanField(default=False, help_text='If the json cache needs updating'),
),
migrations.AlterField(
model_name='anatomyquestion',
name='image',
field=models.ImageField(storage=django.core.files.storage.FileSystemStorage(base_url='http://161.35.163.87//media/anatomy/', location='/home/ross/sites/rad/media//anatomy/'), upload_to=anatomy.models.image_directory_path),
),
]
+3
View File
@@ -195,6 +195,9 @@ class Exam(models.Model):
active = models.BooleanField(help_text="If an exam should be available", active = models.BooleanField(help_text="If an exam should be available",
default=True) default=True)
recreate_json = models.BooleanField(help_text="If the json cache needs updating",
default=False)
def __str__(self): def __str__(self):
return self.name return self.name
@@ -12,6 +12,7 @@
<li>{{ question }}</lid> <li>{{ question }}</lid>
{% endfor %} {% endfor %}
</ol> </ol>
<a href="{% url 'anatomy:exam_json' pk=exam.pk %}">JSON</a>
</div> </div>
{% endblock %} {% endblock %}
+21 -8
View File
@@ -8,7 +8,8 @@ from django.contrib.auth.models import User
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.http import Http404, JsonResponse from django.core.cache import cache
from .forms import AnatomyAnswerForm, MarkAnatomyQuestionForm from .forms import AnatomyAnswerForm, MarkAnatomyQuestionForm
from .models import ( from .models import (
@@ -453,11 +454,18 @@ def active_exams(request):
def exam_json(request, pk): def exam_json(request, pk):
exam = get_object_or_404(Exam, pk=pk) exam = get_object_or_404(Exam, pk=pk)
if not exam.active: if not exam.active:
raise Http404("No available exam") raise Http404("No available exam")
exam_json_cache = cache.get("exam_json_{}".format(pk))
if exam_json_cache is not None and not exam.recreate_json:
return JsonResponse(exam_json)
questions = exam.exam_questions.all() questions = exam.exam_questions.all()
exam_questions = defaultdict(dict) exam_questions = defaultdict(dict)
@@ -470,13 +478,18 @@ def exam_json(request, pk):
"type": "anatomy", "type": "anatomy",
} }
exam_json = { exam_json = {
"eid": exam.id, "eid": exam.id,
"exam_type": "anatomy", "exam_type": "anatomy",
"exam_name": exam.name, "exam_name": exam.name,
"exam_mode": True, "exam_mode": True,
"questions": exam_questions, "questions": exam_questions,
} }
exam.recreate_json = False
cache.set("exam_json_{}".format(pk), exam_json, 3600)
return JsonResponse(exam_json) return JsonResponse(exam_json)
+7
View File
@@ -179,3 +179,10 @@ LOGGING = {
} }
CORS_ORIGIN_ALLOW_ALL = True CORS_ORIGIN_ALLOW_ALL = True
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
}
}