permission and search preference caching

This commit is contained in:
vabene1111
2022-09-16 14:35:35 +02:00
parent dc076d25d6
commit b2a10f269c
2 changed files with 26 additions and 7 deletions

View File

@ -1,7 +1,9 @@
import inspect
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
from django.core.cache import caches
from django.core.cache import cache
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.http import HttpResponseRedirect
from django.urls import reverse, reverse_lazy
@ -41,13 +43,23 @@ def has_group_permission(user, groups):
if not user.is_authenticated:
return False
groups_allowed = get_allowed_groups(groups)
CACHE_KEY = hash((inspect.stack()[0][3], user, groups_allowed))
cached_result = cache.get(CACHE_KEY, default=None)
if cached_result is not None:
return cached_result
result = False
print('running check', user, groups_allowed)
if user.is_authenticated:
if user_space := user.userspace_set.filter(active=True):
if len(user_space) != 1:
return False # do not allow any group permission if more than one space is active, needs to be changed when simultaneous multi-space-tenancy is added
result = False # do not allow any group permission if more than one space is active, needs to be changed when simultaneous multi-space-tenancy is added
if bool(user_space.first().groups.filter(name__in=groups_allowed)):
return True
return False
result = True
cache.set(CACHE_KEY, result, timeout=10)
return result
def is_object_owner(user, obj):
@ -106,7 +118,7 @@ def share_link_valid(recipe, share):
"""
try:
CACHE_KEY = f'recipe_share_{recipe.pk}_{share}'
if c := caches['default'].get(CACHE_KEY, False):
if c := cache.get(CACHE_KEY, False):
return c
if link := ShareLink.objects.filter(recipe=recipe, uuid=share, abuse_blocked=False).first():
@ -114,7 +126,7 @@ def share_link_valid(recipe, share):
return False
link.request_count += 1
link.save()
caches['default'].set(CACHE_KEY, True, timeout=3)
cache.set(CACHE_KEY, True, timeout=3)
return True
return False
except ValidationError:

View File

@ -3,6 +3,7 @@ from collections import Counter
from datetime import date, timedelta
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector, TrigramSimilarity
from django.core.cache import cache
from django.core.cache import caches
from django.db.models import (Avg, Case, Count, Exists, F, Func, Max, OuterRef, Q, Subquery, Value, When, FilteredRelation)
from django.db.models.functions import Coalesce, Lower, Substr
@ -35,7 +36,13 @@ class RecipeSearch():
else:
self._params = {**(params or {})}
if self._request.user.is_authenticated:
CACHE_KEY = f'search_pref_{request.user.id}'
cached_result = cache.get(CACHE_KEY, default=None)
if cached_result is not None:
self._search_prefs = cached_result
else:
self._search_prefs = request.user.searchpreference
cache.set(CACHE_KEY, self._search_prefs, timeout=10)
else:
self._search_prefs = SearchPreference()
self._string = self._params.get('query').strip() if self._params.get('query', None) else None