cache roles on characters

This commit is contained in:
Gabriel Augendre 2024-01-10 10:16:42 +01:00
parent 2fec5b32da
commit 78be4d2728
2 changed files with 42 additions and 0 deletions

View file

@ -14,6 +14,7 @@ from django_extensions.db.models import TimeStampedModel
from character.models import Capability, Path
from character.models.dice import Dice
from character.models.equipment import Weapon
from character.models.utils import cache_on_instance
from common.models import DocumentedModel, UniquelyNamedModel
@ -471,12 +472,15 @@ class Character(models.Model):
pk__in=self.states.all().values_list("pk", flat=True),
)
@cache_on_instance()
def managed_by(self, user):
return self in Character.objects.managed_by(user)
@cache_on_instance()
def mastered_by(self, user):
return self in Character.objects.mastered_by(user)
@cache_on_instance()
def owned_by(self, user):
return self in Character.objects.owned_by(user)

View file

@ -0,0 +1,38 @@
import functools
def cache_on_instance():
def wrapper(func):
@functools.wraps(func)
def inner(self, arg):
cache_name = func.__name__
if (cached := _get_cache_value(self, cache_name, arg)) is not None:
return cached
res = func(self, arg)
_set_cache_value(self, cache_name, arg, res)
return res
return inner
return wrapper
def _get_cache_value(obj, cache_name: str, key: str) -> any:
cache_name += "_cache"
cache = getattr(obj, cache_name, None)
if cache is None:
cache = {}
setattr(obj, cache_name, cache)
return None
return cache.get(key)
def _set_cache_value(obj, cache_name: str, key: str, value: any):
cache_name += "_cache"
cache = getattr(obj, cache_name)
if cache is None:
cache = {}
setattr(obj, cache_name, cache)
cache[key] = value