aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-08-05 17:50:16 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2023-08-05 17:50:16 -0400
commitbd4590d7cfaf2fa554ff6c37e095975ac4eb0f02 (patch)
tree66c4720e46cb7eb066a784a22a4de3544babbafb /lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs
parent6de8bb6b38b716605f0d275d998ca47e33520b7a (diff)
Public prep, and api additions
Diffstat (limited to 'lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs')
-rw-r--r--lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs49
1 files changed, 45 insertions, 4 deletions
diff --git a/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs b/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs
index ffe9108..ff73f19 100644
--- a/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs
+++ b/lib/VNLib.Plugins.Extensions.VNCache/src/RemoteBackedMemoryCache.cs
@@ -192,20 +192,61 @@ namespace VNLib.Plugins.Extensions.VNCache
{
CheckConnected();
- DateTime currentTime = DateTime.UtcNow;
-
//Alloc serialzation buffer
using AddOrUpdateBuffer buffer = new (Client.Config.BufferHeap);
//Serialze the value
serialzer.Serialize(value, buffer);
+ //Call update on raw data
+ await AddOrUpdateAsync(key, newKey, buffer, cancellation);
+ }
+
+ ///<inheritdoc/>
+ public override async Task GetAsync(string key, IObjectData rawData, CancellationToken cancellation)
+ {
+ CheckConnected();
+
+ IBlobCacheBucket bucket = _memCache.GetBucket(key);
+
+ //Obtain cache handle
+ using (CacheBucketHandle handle = await bucket.WaitAsync(cancellation))
+ {
+ //Try to read the value
+ if (handle.Cache.TryGetValue(key, out CacheEntry entry))
+ {
+ rawData.SetData(entry.GetDataSegment());
+ return;
+ }
+ }
+
+ //Get the object from the server
+ await Client.GetObjectAsync(key, rawData, cancellation);
+
+ //See if object data was set
+ if (rawData.GetData().IsEmpty)
+ {
+ return;
+ }
+
+ //Update local cache
+ await _memCache.AddOrUpdateObjectAsync(key, null, static b => b.GetData(), rawData, DateTime.UtcNow, CancellationToken.None);
+ }
+
+ ///<inheritdoc/>
+ public override async Task AddOrUpdateAsync(string key, string? newKey, IObjectData rawData, ICacheObjectSerialzer serialzer, CancellationToken cancellation)
+ {
+ CheckConnected();
+
+ 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);
+ await Client.AddOrUpdateObjectAsync(key, newKey, rawData, cancellation);
- await _memCache.AddOrUpdateObjectAsync(key, newKey, static b => b.GetData(), buffer, currentTime, CancellationToken.None);
+ //Safe to update local cache
+ await _memCache.AddOrUpdateObjectAsync(key, newKey, static b => b.GetData(), rawData, currentTime, CancellationToken.None);
}
catch
{