mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-18 12:12:19 +03:00
fix: Use custom cache for better memory usage
This commit is contained in:
parent
d87ad2366b
commit
2f230c2e85
|
@ -1,8 +1,6 @@
|
||||||
"""
|
"""
|
||||||
Provides a set of pluggable permission policies.
|
Provides a set of pluggable permission policies.
|
||||||
"""
|
"""
|
||||||
from functools import lru_cache
|
|
||||||
|
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
|
||||||
from rest_framework import exceptions
|
from rest_framework import exceptions
|
||||||
|
@ -11,13 +9,20 @@ SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS')
|
||||||
|
|
||||||
|
|
||||||
class PermissionCacheMixin:
|
class PermissionCacheMixin:
|
||||||
@lru_cache()
|
def __init__(self):
|
||||||
def has_permission_value(self, request, view):
|
self._cache = {}
|
||||||
return self.has_permission(request, view)
|
|
||||||
|
def has_permission_value(self, request, view):
|
||||||
|
key = (request, view)
|
||||||
|
if key not in self._cache:
|
||||||
|
self._cache[key] = self.has_permission(request, view)
|
||||||
|
return self._cache[key]
|
||||||
|
|
||||||
@lru_cache()
|
|
||||||
def has_object_permission_value(self, request, view, obj):
|
def has_object_permission_value(self, request, view, obj):
|
||||||
return self.has_object_permission(request, view, obj)
|
key = (request, view, obj)
|
||||||
|
if key not in self._cache:
|
||||||
|
self._cache[key] = self.has_object_permission(request, view, obj)
|
||||||
|
return self._cache[key]
|
||||||
|
|
||||||
|
|
||||||
class OperationHolderMixin:
|
class OperationHolderMixin:
|
||||||
|
@ -69,6 +74,7 @@ class OperandHolder(OperationHolderMixin):
|
||||||
|
|
||||||
class AND(PermissionCacheMixin):
|
class AND(PermissionCacheMixin):
|
||||||
def __init__(self, op1, op2):
|
def __init__(self, op1, op2):
|
||||||
|
super().__init__()
|
||||||
self.op1 = op1
|
self.op1 = op1
|
||||||
self.op2 = op2
|
self.op2 = op2
|
||||||
|
|
||||||
|
@ -87,6 +93,7 @@ class AND(PermissionCacheMixin):
|
||||||
|
|
||||||
class OR(PermissionCacheMixin):
|
class OR(PermissionCacheMixin):
|
||||||
def __init__(self, op1, op2):
|
def __init__(self, op1, op2):
|
||||||
|
super().__init__()
|
||||||
self.op1 = op1
|
self.op1 = op1
|
||||||
self.op2 = op2
|
self.op2 = op2
|
||||||
|
|
||||||
|
@ -108,6 +115,7 @@ class OR(PermissionCacheMixin):
|
||||||
|
|
||||||
class NOT(PermissionCacheMixin):
|
class NOT(PermissionCacheMixin):
|
||||||
def __init__(self, op1):
|
def __init__(self, op1):
|
||||||
|
super().__init__()
|
||||||
self.op1 = op1
|
self.op1 = op1
|
||||||
|
|
||||||
def has_permission(self, request, view):
|
def has_permission(self, request, view):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user