UserPermissions should user select_for_update to keep cache reliable

This commit is contained in:
Laura Klünder 2018-12-07 22:14:20 +01:00
parent 26d57d318f
commit 1893b3fd40

View file

@ -1,6 +1,6 @@
from django.conf import settings from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.db import models from django.db import models, transaction
from django.utils.functional import lazy from django.utils.functional import lazy
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -53,17 +53,19 @@ class UserPermissions(models.Model):
break break
if result: if result:
return result return result
try: with transaction.atomic():
result = user.permissions result = cls.objects.filter(user=user).select_for_update().first()
except AttributeError: if not result:
result = cls(user=user) result = cls(user=user)
cache.set(cache_key, result, 900) cache.set(cache_key, result, 900)
return result return result
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super().save(*args, **kwargs) with transaction.atomic():
cache_key = self.get_cache_key(self.pk) UserPermissions.objects.filter(user_id=self.user_id).select_for_update()
cache.set(cache_key, self, 900) super().save(*args, **kwargs)
cache_key = self.get_cache_key(self.pk)
cache.set(cache_key, self, 900)
@property @property
def can_access_base_mapdata(self): def can_access_base_mapdata(self):