From 3564fcc360477e2001639954457c7ad74dfc2b61 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Mon, 12 Aug 2024 11:25:27 +0200 Subject: [PATCH] added redis & django toolbar --- backend/videoflix/content/views.py | 6 ++++++ backend/videoflix/videoflix/settings.py | 20 ++++++++++++++++++++ backend/videoflix/videoflix/urls.py | 3 ++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/videoflix/content/views.py b/backend/videoflix/content/views.py index 3d42bb4..e5ebf7a 100644 --- a/backend/videoflix/content/views.py +++ b/backend/videoflix/content/views.py @@ -4,10 +4,16 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from .serializer import VideoSerializer from .models import Video +from django.core.cache.backends.base import DEFAULT_TIMEOUT +from django.views.decorators.cache import cache_page +from django.conf import settings + +CACHETTL = getattr(settings, 'CACHE_TTL', DEFAULT_TIMEOUT) # Create your views here. @api_view(['GET']) @permission_classes([IsAuthenticated]) +@cache_page(CACHETTL) def video_list(request): if request.method == 'GET': diff --git a/backend/videoflix/videoflix/settings.py b/backend/videoflix/videoflix/settings.py index 9f68b5f..0bfcb90 100644 --- a/backend/videoflix/videoflix/settings.py +++ b/backend/videoflix/videoflix/settings.py @@ -51,11 +51,13 @@ INSTALLED_APPS = [ 'rest_framework.authtoken', 'rest_framework', 'corsheaders', + 'debug_toolbar', 'content.apps.ContentConfig', 'users', ] MIDDLEWARE = [ + 'debug_toolbar.middleware.DebugToolbarMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', @@ -84,6 +86,24 @@ TEMPLATES = [ }, ] +CACHES = { + "default": { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": "redis://127.0.0.1:6379/1", + "OPTIONS": { + "PASSWORD": 'foobared', + "CLIENT_CLASS": "django_redis.client.DefaultClient" + }, + "KEY_PREFIX": "videoflix" + } +} + +INTERNAL_IPS = [ + '127.0.0.1', +] + +CACHE_TTL = 60 * 15 + MEDIA_ROOT = os.path.join(BASE_DIR, 'media') THUMBNAIL_DIR = os.path.join(BASE_DIR, 'media/img') MEDIA_URL = '/media/' diff --git a/backend/videoflix/videoflix/urls.py b/backend/videoflix/videoflix/urls.py index 4019a93..b288903 100644 --- a/backend/videoflix/videoflix/urls.py +++ b/backend/videoflix/videoflix/urls.py @@ -28,6 +28,7 @@ from auth.views import ( ChangePasswordView ) from content import views as content_views +from debug_toolbar.toolbar import debug_toolbar_urls urlpatterns = [ path('admin/', admin.site.urls), @@ -46,4 +47,4 @@ urlpatterns = [ path('auth/verify-email/', VerifyEmailView.as_view()), path('auth/forgot-password/', ForgotPasswordView.as_view()), path('auth/change-password/', ChangePasswordView.as_view()), -] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + debug_toolbar_urls() \ No newline at end of file