[backend/core] Use upsert for cache store instead of detecting conflicts (ISH-212)
This commit is contained in:
parent
e3e34b4495
commit
7e89c0e4c4
2 changed files with 11 additions and 15 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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"/>
|
||||
|
|
Loading…
Add table
Reference in a new issue