Skip to content

Commit

Permalink
✨(api) check if cache changed before adding a cache entry
Browse files Browse the repository at this point in the history
Prolonged computation times for certain indicators can bring redundant cache
entries from parallel computations.
Before adding a cache entry, we now check if an identical one already exists,
similar to Django's `get_or_create` mechanism.
  • Loading branch information
wilbrdt committed May 16, 2024
1 parent ebcbda2 commit e5f0d34
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api/core/warren/indicators/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ async def get_or_compute(self, update: bool = False):
if issubclass(self._compute_annotation, BaseModel):
value = value.json()

# Cache entry may have been created during a long compute time
cache = await self.get_cache()

if cache is None:
cache = CacheEntry.parse_obj(
CacheEntryCreate(key=self.cache_key, value=value)
Expand Down

0 comments on commit e5f0d34

Please sign in to comment.