Skip to content

Commit 65ac6d9

Browse files
committed
Try again in cache race condition update_or_create
1 parent 814760b commit 65ac6d9

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

bolt-cache/bolt/cache/core.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ def set(self, value, expiration: datetime | timedelta | int | float | None = Non
6969
key=self.key, defaults=defaults
7070
)
7171
except IntegrityError:
72-
# Most likely a race condition in creating the item
73-
# so we'll effectively do a get and return the stored value
74-
self.reload()
75-
return self.value
72+
# Most likely a race condition in creating the item,
73+
# so trying again should do an update
74+
with transaction.atomic():
75+
item, _ = self._model_class.objects.update_or_create(
76+
key=self.key, defaults=defaults
77+
)
7678

7779
self.reload()
7880
return item.value

0 commit comments

Comments
 (0)