From 9c9e00515a367a7c4a916d19c3d71795f2193910 Mon Sep 17 00:00:00 2001 From: vnugent Date: Mon, 7 Aug 2023 20:08:18 -0400 Subject: New api impl patch --- .../src/RemoteBackedMemoryCache.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs') diff --git a/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs b/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs index ff73f19..0e92c22 100644 --- a/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs +++ b/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs @@ -198,8 +198,22 @@ namespace VNLib.Plugins.Extensions.VNCache //Serialze the value serialzer.Serialize(value, buffer); - //Call update on raw data - await AddOrUpdateAsync(key, newKey, buffer, cancellation); + DateTime currentTime = DateTime.UtcNow; + + try + { + //Update remote first, and if exceptions are raised, do not update local cache + await Client.AddOrUpdateObjectAsync(key, newKey, (IObjectData)buffer, cancellation); + + //Safe to update local cache + await _memCache.AddOrUpdateObjectAsync(key, newKey, static b => b.GetData(), buffer, currentTime, CancellationToken.None); + } + catch + { + //Remove local cache if exception occurs + await _memCache.DeleteObjectAsync(key, CancellationToken.None); + throw; + } } /// -- cgit