Skip to content

armgit / plane

Public Imported from GitHub
0

πŸ”₯πŸ”₯πŸ”₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.

fix: caching (#3923)

Nikhilb57c389c7579
diff --git a/apiserver/bin/takeoff b/apiserver/bin/takeoff
index efea53f874..5a1da1570a 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 8f62370ecf..3194009b2c 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 0000000000..4dfbe6c103
--- /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 dba89c4a62..aece1d644f 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)