forked from github/plane
fix: caching (#3923)
This commit is contained in:
parent
27acd1bec1
commit
b57c389c75
@ -21,11 +21,15 @@ SIGNATURE=$(echo "$HOSTNAME$MAC_ADDRESS$CPU_INFO$MEMORY_INFO$DISK_INFO" | sha256
|
|||||||
export MACHINE_SIGNATURE=$SIGNATURE
|
export MACHINE_SIGNATURE=$SIGNATURE
|
||||||
|
|
||||||
# Register instance
|
# Register instance
|
||||||
python manage.py register_instance $MACHINE_SIGNATURE
|
python manage.py register_instance "$MACHINE_SIGNATURE"
|
||||||
|
|
||||||
# Load the configuration variable
|
# Load the configuration variable
|
||||||
python manage.py configure_instance
|
python manage.py configure_instance
|
||||||
|
|
||||||
# Create the default bucket
|
# Create the default bucket
|
||||||
python manage.py create_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 -
|
||||||
|
@ -21,12 +21,15 @@ SIGNATURE=$(echo "$HOSTNAME$MAC_ADDRESS$CPU_INFO$MEMORY_INFO$DISK_INFO" | sha256
|
|||||||
export MACHINE_SIGNATURE=$SIGNATURE
|
export MACHINE_SIGNATURE=$SIGNATURE
|
||||||
|
|
||||||
# Register instance
|
# Register instance
|
||||||
python manage.py register_instance $MACHINE_SIGNATURE
|
python manage.py register_instance "$MACHINE_SIGNATURE"
|
||||||
# Load the configuration variable
|
# Load the configuration variable
|
||||||
python manage.py configure_instance
|
python manage.py configure_instance
|
||||||
|
|
||||||
# Create the default bucket
|
# Create the default bucket
|
||||||
python manage.py create_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
|
python manage.py runserver 0.0.0.0:8000 --settings=plane.settings.local
|
||||||
|
|
||||||
|
17
apiserver/plane/db/management/commands/clear_cache.py
Normal file
17
apiserver/plane/db/management/commands/clear_cache.py
Normal file
@ -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
|
@ -1,7 +1,11 @@
|
|||||||
from django.core.cache import cache
|
# Python imports
|
||||||
# from django.utils.encoding import force_bytes
|
|
||||||
# import hashlib
|
|
||||||
from functools import wraps
|
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
|
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):
|
def _wrapped_view(instance, request, *args, **kwargs):
|
||||||
# Function to generate cache key
|
# Function to generate cache key
|
||||||
auth_header = (
|
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()
|
custom_path = path if path is not None else request.get_full_path()
|
||||||
key = generate_cache_key(custom_path, auth_header)
|
key = generate_cache_key(custom_path, auth_header)
|
||||||
cached_result = cache.get(key)
|
cached_result = cache.get(key)
|
||||||
if cached_result is not None:
|
if cached_result is not None:
|
||||||
print("Cache Hit")
|
|
||||||
return Response(
|
return Response(
|
||||||
cached_result["data"], status=cached_result["status"]
|
cached_result["data"], status=cached_result["status"]
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Cache Miss")
|
|
||||||
response = view_func(instance, request, *args, **kwargs)
|
response = view_func(instance, request, *args, **kwargs)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200 and not settings.DEBUG:
|
||||||
cache.set(
|
cache.set(
|
||||||
key,
|
key,
|
||||||
{"data": response.data, "status": response.status_code},
|
{"data": response.data, "status": response.status_code},
|
||||||
@ -71,11 +74,12 @@ def invalidate_cache(path=None, url_params=False, user=True):
|
|||||||
)
|
)
|
||||||
|
|
||||||
auth_header = (
|
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)
|
key = generate_cache_key(custom_path, auth_header)
|
||||||
cache.delete(key)
|
cache.delete(key)
|
||||||
print("Invalidating cache")
|
|
||||||
# Execute the view function
|
# Execute the view function
|
||||||
return view_func(instance, request, *args, **kwargs)
|
return view_func(instance, request, *args, **kwargs)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user