[WEB - 471] dev: caching users and workspace apis (#3707)
* dev: caching users and workspace apis * dev: cache user and config apis * dev: update caching function to use user_id instead of token * dev: update caching layer * dev: update caching logic * dev: format caching file * dev: refactor caching to include name space and user id as key * dev: cache project cover image endpoint
This commit is contained in:
parent
549f6d0943
commit
ed8782757d
10 changed files with 189 additions and 44 deletions
84
apiserver/plane/utils/cache.py
Normal file
84
apiserver/plane/utils/cache.py
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
from django.core.cache import cache
|
||||
# from django.utils.encoding import force_bytes
|
||||
# import hashlib
|
||||
from functools import wraps
|
||||
from rest_framework.response import Response
|
||||
|
||||
|
||||
def generate_cache_key(custom_path, auth_header=None):
|
||||
"""Generate a cache key with the given params"""
|
||||
if auth_header:
|
||||
key_data = f"{custom_path}:{auth_header}"
|
||||
else:
|
||||
key_data = custom_path
|
||||
return key_data
|
||||
|
||||
|
||||
def cache_response(timeout=60 * 60, path=None, user=True):
|
||||
"""decorator to create cache per user"""
|
||||
|
||||
def decorator(view_func):
|
||||
@wraps(view_func)
|
||||
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
|
||||
)
|
||||
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:
|
||||
cache.set(
|
||||
key,
|
||||
{"data": response.data, "status": response.status_code},
|
||||
timeout,
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
return _wrapped_view
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def invalidate_cache(path=None, url_params=False, user=True):
|
||||
"""invalidate cache per user"""
|
||||
|
||||
def decorator(view_func):
|
||||
@wraps(view_func)
|
||||
def _wrapped_view(instance, request, *args, **kwargs):
|
||||
# Invalidate cache before executing the view function
|
||||
if url_params:
|
||||
path_with_values = path
|
||||
for key, value in kwargs.items():
|
||||
path_with_values = path_with_values.replace(
|
||||
f":{key}", str(value)
|
||||
)
|
||||
|
||||
custom_path = path_with_values
|
||||
else:
|
||||
custom_path = (
|
||||
path if path is not None else request.get_full_path()
|
||||
)
|
||||
|
||||
auth_header = (
|
||||
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)
|
||||
|
||||
return _wrapped_view
|
||||
|
||||
return decorator
|
||||
Loading…
Add table
Add a link
Reference in a new issue