diff --git a/api/dashboard/user/dash_user_views.py b/api/dashboard/user/dash_user_views.py index e285d3e5..5abe83c7 100644 --- a/api/dashboard/user/dash_user_views.py +++ b/api/dashboard/user/dash_user_views.py @@ -13,6 +13,7 @@ from utils.types import RoleType, WebHookActions, WebHookCategory from utils.utils import CommonUtils, DateTimeUtils, DiscordWebhooks, send_template_mail from . import dash_user_serializer +from django.core.cache import cache BE_DOMAIN_NAME = decouple_config("BE_DOMAIN_NAME") @@ -22,8 +23,10 @@ class UserInfoAPI(APIView): def get(self, request): user_muid = JWTUtils.fetch_muid(request) - user = User.objects.filter(muid=user_muid).first() - + user = cache.get(f"db_user_{user_muid}") + if not user: + user = User.objects.filter(muid=user_muid).first() + cache.set(f"db_user_{user_muid}", user, timeout=10) if user is None: return CustomResponse( general_message="No user data available" diff --git a/api/register/register_views.py b/api/register/register_views.py index 4f66fd59..e5df03e6 100644 --- a/api/register/register_views.py +++ b/api/register/register_views.py @@ -2,6 +2,7 @@ from rest_framework.views import APIView from db.organization import Country, Department, District, Organization, State, Zone +from django.utils.decorators import method_decorator from db.task import InterestGroup from db.user import Role, User from utils.response import CustomResponse @@ -9,6 +10,8 @@ from utils.utils import send_template_mail from . import serializers from .register_helper import get_auth_token +from django.views.decorators.cache import cache_page +from django.core.cache import cache class UserRegisterValidateAPI(APIView): @@ -24,12 +27,14 @@ def put(self, request): class RoleAPI(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): roles = Role.objects.all().values("id", "title") return CustomResponse(response={"roles": roles}).get_success_response() class CollegesAPI(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): colleges = Organization.objects.filter( org_type=OrganizationType.COLLEGE.value @@ -39,6 +44,7 @@ def get(self, request): class DepartmentAPI(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): department_serializer = Department.objects.all().values("id", "title") @@ -52,6 +58,7 @@ def get(self, request): class CompanyAPI(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): company_queryset = Organization.objects.filter( org_type=OrganizationType.COMPANY.value @@ -76,7 +83,7 @@ def post(self, request): return CustomResponse(general_message="Invalid muid").get_failure_response() serializer = serializers.LearningCircleUserSerializer(user) - id, muid, full_name, email, phone = serializer.data.values() + id, muid, full_name, email, phone = serializer.data.values() name = full_name @@ -103,8 +110,9 @@ def post(self, request): if not create_user.is_valid(): return CustomResponse(message=create_user.errors).get_failure_response() - + user = create_user.save() + cache.set(f"db_user_{user.muid}", user, timeout=20) password = request.data["user"]["password"] res_data = get_auth_token(user.muid, password) @@ -123,6 +131,7 @@ def post(self, request): class CountryAPI(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): countries = Country.objects.all() @@ -203,6 +212,7 @@ def post(self, request): class CommunityAPI(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): community_queryset = Organization.objects.filter( org_type=OrganizationType.COMMUNITY.value @@ -218,6 +228,7 @@ def get(self, request): class AreaOfInterestAPI(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): aoi_queryset = InterestGroup.objects.all() @@ -245,6 +256,7 @@ def post(self, request): class UserCountryAPI(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): country = Country.objects.all() diff --git a/mulearnbackend/settings.py b/mulearnbackend/settings.py index 1b35df38..0854c5d4 100644 --- a/mulearnbackend/settings.py +++ b/mulearnbackend/settings.py @@ -10,7 +10,6 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ """ - import os from pathlib import Path @@ -94,11 +93,14 @@ WSGI_APPLICATION = "mulearnbackend.wsgi.application" +REDIS_HOST = decouple_config("REDIS_HOST") +REDIS_PORT = decouple_config("REDIS_PORT") + CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { - "hosts": [(decouple_config("REDIS_HOST"), decouple_config("REDIS_PORT"))], + "hosts": [(REDIS_HOST, REDIS_PORT)], }, }, } @@ -117,6 +119,23 @@ } } +CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.db.DatabaseCache", + "LOCATION": "my_cache_table", + }, + "redis": { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": f"redis://{REDIS_HOST}:{REDIS_PORT}/1", + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.DefaultClient", + }, + }, +} + +# Use the Redis cache as the default cache +CACHES["default"] = CACHES["redis"] + # Password validation # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators