From a0bb8ca90da6b2610322ca4a6f985ea79a00c372 Mon Sep 17 00:00:00 2001 From: Peter van Doorn <71013416+two-trick-pony-NL@users.noreply.github.com> Date: Fri, 19 May 2023 16:01:48 +0200 Subject: [PATCH] added cache --- AWS/deploymentconfig.json | 4 ++++ articles/views.py | 4 ++++ core/settings.py | 7 +++++++ homepage/views.py | 6 ++++++ machinelearning/views.py | 6 +++--- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/AWS/deploymentconfig.json b/AWS/deploymentconfig.json index 41b9584..c466259 100644 --- a/AWS/deploymentconfig.json +++ b/AWS/deploymentconfig.json @@ -5,5 +5,9 @@ "nginx": { "image": ":tldrnews.nginx.latest", "ports": {"80": "HTTP"} + }, + "redis-cache": { + "image": "redis/redis-stack:latest", + "ports": {"6379": "HTTP", "8001":"HTTP"} } } \ No newline at end of file diff --git a/articles/views.py b/articles/views.py index b31291f..bb239d1 100644 --- a/articles/views.py +++ b/articles/views.py @@ -2,16 +2,20 @@ from .models import Article from newssources.models import Source from django.contrib.auth.decorators import login_required +from django.views.decorators.cache import cache_page + # Create your views here. +@cache_page(60 * 15) def article_by_slug(request, slug): article = Article.objects.filter(slug=slug).last() sources = article.sources.all() context = {'article':article, 'sources':sources} return render(request, "article_index.html", context) +@cache_page(60 * 15) def article_by_id(request, id): article = Article.objects.get(id=id) sources = article.sources.all() diff --git a/core/settings.py b/core/settings.py index 7782fd7..616551e 100644 --- a/core/settings.py +++ b/core/settings.py @@ -103,6 +103,13 @@ }, }} +#Setting location of cache server (Redis in our case) +CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.redis.RedisCache", + "LOCATION": "redis://127.0.0.1:6379", + } +} # Password validation # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators diff --git a/homepage/views.py b/homepage/views.py index 25f5608..9c98aa2 100644 --- a/homepage/views.py +++ b/homepage/views.py @@ -1,8 +1,11 @@ from django.shortcuts import render from articles.models import Article from django.db.models import Count +from django.views.decorators.cache import cache_page + # Create your views here. +@cache_page(60 * 15) def index(request): articles = Article.objects.filter(published=True, created_by_ai=True).order_by('-sources') #articles = Article.objects.annotate(most_sources=Count("sources")).order_by("most_sources") @@ -13,14 +16,17 @@ def index(request): context = {'hero':hero, 'highlighted':highlighted, 'frontpage':frontpage, 'older':older} return render(request, "homepage.html", context) +@cache_page(60 * 15) def faq(request): context = {} return render(request, "faq_index.html", context) +@cache_page(60 * 15) def funding(request): context = {} return render(request, "funding_index.html", context) +@cache_page(60 * 15) def responsibleai(request): context = {} return render(request, "ai_index.html", context) \ No newline at end of file diff --git a/machinelearning/views.py b/machinelearning/views.py index 10b5982..f33c287 100644 --- a/machinelearning/views.py +++ b/machinelearning/views.py @@ -9,9 +9,9 @@ if ENABLE_AI: print(ENABLE_AI) - import numpy as np - from sentence_transformers import SentenceTransformer - sentencemodel = SentenceTransformer('sentence-transformers/all-mpnet-base-v1') + #import numpy as np + #from sentence_transformers import SentenceTransformer + #sentencemodel = SentenceTransformer('sentence-transformers/all-mpnet-base-v1') else: pass