From b57c389c757902c6604243e48f99045893c3da28 Mon Sep 17 00:00:00 2001 From: Nikhil <118773738+pablohashescobar@users.noreply.github.com> Date: Mon, 11 Mar 2024 21:04:43 +0530 Subject: [PATCH] fix: caching (#3923) --- apiserver/bin/takeoff | 8 +++++-- apiserver/bin/takeoff.local | 5 +++- .../db/management/commands/clear_cache.py | 17 +++++++++++++ apiserver/plane/utils/cache.py | 24 +++++++++++-------- 4 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 apiserver/plane/db/management/commands/clear_cache.py diff --git a/apiserver/bin/takeoff b/apiserver/bin/takeoff index efea53f87..5a1da1570 100755 --- a/apiserver/bin/takeoff +++ b/apiserver/bin/takeoff @@ -21,11 +21,15 @@ SIGNATURE=$(echo "$HOSTNAME$MAC_ADDRESS$CPU_INFO$MEMORY_INFO$DISK_INFO" | sha256 export MACHINE_SIGNATURE=$SIGNATURE # Register instance -python manage.py register_instance $MACHINE_SIGNATURE +python manage.py register_instance "$MACHINE_SIGNATURE" + # Load the configuration variable python manage.py configure_instance # Create the default bucket python manage.py create_bucket -exec gunicorn -w $GUNICORN_WORKERS -k uvicorn.workers.UvicornWorker plane.asgi:application --bind 0.0.0.0:${PORT:-8000} --max-requests 1200 --max-requests-jitter 1000 --access-logfile - +# Clear Cache before starting to remove stale values +python manage.py clear_cache + +exec gunicorn -w "$GUNICORN_WORKERS" -k uvicorn.workers.UvicornWorker plane.asgi:application --bind 0.0.0.0:"${PORT:-8000}" --max-requests 1200 --max-requests-jitter 1000 --access-logfile - diff --git a/apiserver/bin/takeoff.local b/apiserver/bin/takeoff.local index 8f62370ec..3194009b2 100755 --- a/apiserver/bin/takeoff.local +++ b/apiserver/bin/takeoff.local @@ -21,12 +21,15 @@ SIGNATURE=$(echo "$HOSTNAME$MAC_ADDRESS$CPU_INFO$MEMORY_INFO$DISK_INFO" | sha256 export MACHINE_SIGNATURE=$SIGNATURE # Register instance -python manage.py register_instance $MACHINE_SIGNATURE +python manage.py register_instance "$MACHINE_SIGNATURE" # Load the configuration variable python manage.py configure_instance # Create the default bucket python manage.py create_bucket +# Clear Cache before starting to remove stale values +python manage.py clear_cache + python manage.py runserver 0.0.0.0:8000 --settings=plane.settings.local diff --git a/apiserver/plane/db/management/commands/clear_cache.py b/apiserver/plane/db/management/commands/clear_cache.py new file mode 100644 index 000000000..4dfbe6c10 --- /dev/null +++ b/apiserver/plane/db/management/commands/clear_cache.py @@ -0,0 +1,17 @@ +# Django imports +from django.core.cache import cache +from django.core.management import BaseCommand + + +class Command(BaseCommand): + help = "Clear Cache before starting the server to remove stale values" + + def handle(self, *args, **options): + try: + cache.clear() + self.stdout.write(self.style.SUCCESS("Cache Cleared")) + return + except Exception: + # Another ClientError occurred + self.stdout.write(self.style.ERROR("Failed to clear cache")) + return diff --git a/apiserver/plane/utils/cache.py b/apiserver/plane/utils/cache.py index dba89c4a6..aece1d644 100644 --- a/apiserver/plane/utils/cache.py +++ b/apiserver/plane/utils/cache.py @@ -1,7 +1,11 @@ -from django.core.cache import cache -# from django.utils.encoding import force_bytes -# import hashlib +# Python imports from functools import wraps + +# Django imports +from django.conf import settings +from django.core.cache import cache + +# Third party imports from rest_framework.response import Response @@ -22,21 +26,20 @@ def cache_response(timeout=60 * 60, path=None, user=True): def _wrapped_view(instance, request, *args, **kwargs): # Function to generate cache key auth_header = ( - None if request.user.is_anonymous else str(request.user.id) if user else None + None + if request.user.is_anonymous + else str(request.user.id) if user else None ) custom_path = path if path is not None else request.get_full_path() key = generate_cache_key(custom_path, auth_header) cached_result = cache.get(key) if cached_result is not None: - print("Cache Hit") return Response( cached_result["data"], status=cached_result["status"] ) - - print("Cache Miss") response = view_func(instance, request, *args, **kwargs) - if response.status_code == 200: + if response.status_code == 200 and not settings.DEBUG: cache.set( key, {"data": response.data, "status": response.status_code}, @@ -71,11 +74,12 @@ def invalidate_cache(path=None, url_params=False, user=True): ) auth_header = ( - None if request.user.is_anonymous else str(request.user.id) if user else None + None + if request.user.is_anonymous + else str(request.user.id) if user else None ) key = generate_cache_key(custom_path, auth_header) cache.delete(key) - print("Invalidating cache") # Execute the view function return view_func(instance, request, *args, **kwargs)