UserPermissions should user select_for_update to keep cache reliable
This commit is contained in:
parent
26d57d318f
commit
1893b3fd40
1 changed files with 11 additions and 9 deletions
|
@ -1,6 +1,6 @@
|
|||
from django.conf import settings
|
||||
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.translation import ugettext_lazy as _
|
||||
|
||||
|
@ -53,14 +53,16 @@ class UserPermissions(models.Model):
|
|||
break
|
||||
if result:
|
||||
return result
|
||||
try:
|
||||
result = user.permissions
|
||||
except AttributeError:
|
||||
with transaction.atomic():
|
||||
result = cls.objects.filter(user=user).select_for_update().first()
|
||||
if not result:
|
||||
result = cls(user=user)
|
||||
cache.set(cache_key, result, 900)
|
||||
return result
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
with transaction.atomic():
|
||||
UserPermissions.objects.filter(user_id=self.user_id).select_for_update()
|
||||
super().save(*args, **kwargs)
|
||||
cache_key = self.get_cache_key(self.pk)
|
||||
cache.set(cache_key, self, 900)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue