Skip to content

Commit

Permalink
Try again in cache race condition update_or_create
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Feb 2, 2024
1 parent 814760b commit 65ac6d9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions bolt-cache/bolt/cache/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ def set(self, value, expiration: datetime | timedelta | int | float | None = Non
key=self.key, defaults=defaults
)
except IntegrityError:
# Most likely a race condition in creating the item
# so we'll effectively do a get and return the stored value
self.reload()
return self.value
# Most likely a race condition in creating the item,
# so trying again should do an update
with transaction.atomic():
item, _ = self._model_class.objects.update_or_create(
key=self.key, defaults=defaults
)

self.reload()
return item.value
Expand Down

0 comments on commit 65ac6d9

Please sign in to comment.