[backend/core] Use upsert for cache store instead of detecting conflicts (ISH-212)

This commit is contained in:
Laura Hausmann 2024-03-23 19:02:12 +01:00
parent e3e34b4495
commit 7e89c0e4c4
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 11 additions and 15 deletions

View file

@ -147,21 +147,16 @@ public class CacheService([FromKeyedServices("cache")] DatabaseContext db)
Expiry = expiry,
Ttl = ttl
};
db.Add(entity);
try
{
await db.SaveChangesAsync();
}
catch (UniqueConstraintException)
{
db.Remove(entity);
entity = await db.CacheStore.FirstOrDefaultAsync(p => p.Key == key) ??
throw new Exception("Failed to fetch entity after UniqueConstraintException");
entity.Value = value;
entity.Expiry = expiry;
entity.Ttl = ttl;
await db.SaveChangesAsync();
}
await db.CacheStore.Upsert(entity)
.On(p => p.Key)
.WhenMatched((_, orig) => new CacheEntry
{
Value = orig.Value,
Expiry = orig.Expiry,
Ttl = orig.Ttl
})
.RunAsync();
}
}

View file

@ -25,6 +25,7 @@
<PackageReference Include="dotNetRdf.Core" Version="3.2.6-iceshrimp"/>
<PackageReference Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="8.0.0"/>
<PackageReference Include="EntityFrameworkCore.Projectables" Version="3.0.4"/>
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="8.0.0" />
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0"/>
<PackageReference Include="libsodium" Version="1.0.18.4"/>
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="8.0.1"/>