aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheExtensions.cs10
-rw-r--r--lib/VNLib.Data.Caching/src/GlobalCacheExtensions.cs48
-rw-r--r--lib/VNLib.Data.Caching/src/IGlobalCacheProvider.cs44
-rw-r--r--lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs21
-rw-r--r--lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/ScopedCache.cs4
5 files changed, 104 insertions, 23 deletions
diff --git a/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheExtensions.cs b/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheExtensions.cs
index ded89d2..4cacb3f 100644
--- a/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheExtensions.cs
+++ b/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheExtensions.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Data.Caching.ObjectCache
@@ -144,14 +144,14 @@ namespace VNLib.Data.Caching.ObjectCache
this IBlobCacheTable table,
string objectId,
string? alternateId,
- ObjectDataReader<T> bodyData,
+ ObjectDataGet<T> bodyData,
T state,
DateTime time,
CancellationToken cancellation = default)
{
-
- _ = table ?? throw new ArgumentNullException(nameof(table));
- _ = bodyData ?? throw new ArgumentNullException(nameof(bodyData));
+ ArgumentNullException.ThrowIfNull(table);
+ ArgumentNullException.ThrowIfNull(bodyData);
+ ArgumentException.ThrowIfNullOrWhiteSpace(objectId);
//See if an id change is required
if (string.IsNullOrWhiteSpace(alternateId))
diff --git a/lib/VNLib.Data.Caching/src/GlobalCacheExtensions.cs b/lib/VNLib.Data.Caching/src/GlobalCacheExtensions.cs
index 586df73..203219c 100644
--- a/lib/VNLib.Data.Caching/src/GlobalCacheExtensions.cs
+++ b/lib/VNLib.Data.Caching/src/GlobalCacheExtensions.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Data.Caching
@@ -26,7 +26,8 @@ using System;
using System.Threading;
using System.Threading.Tasks;
-#pragma warning disable CA1062 // Validate arguments of public methods
+using VNLib.Net.Messaging.FBM;
+using VNLib.Data.Caching.Exceptions;
namespace VNLib.Data.Caching
{
@@ -46,6 +47,7 @@ namespace VNLib.Data.Caching
/// <returns>A task that complets when the object data has been written to the data buffer</returns>
public static Task GetAsync(this IGlobalCacheProvider cache, string key, IObjectData rawData, CancellationToken cancellation)
{
+ ArgumentNullException.ThrowIfNull(cache);
return cache.GetAsync(key, static (cd, data) => cd.SetData(data), rawData, cancellation);
}
@@ -61,6 +63,7 @@ namespace VNLib.Data.Caching
/// <returns>A task that completes when the update operation has compelted</returns>
public static Task AddOrUpdateAsync(this IGlobalCacheProvider cache, string key, string? newKey, IObjectData rawData, CancellationToken cancellation)
{
+ ArgumentNullException.ThrowIfNull(cache);
return cache.AddOrUpdateAsync(key, newKey, static cd => cd.GetData(), rawData, cancellation);
}
@@ -76,10 +79,49 @@ namespace VNLib.Data.Caching
/// <returns>A task that completes when the update operation has compelted</returns>
public static Task AddOrUpdateAsync(this IGlobalCacheProvider cache, string key, string? newKey, ReadOnlyMemory<byte> rawData, CancellationToken cancellation)
{
+ ArgumentNullException.ThrowIfNull(cache);
return cache.AddOrUpdateAsync(key, newKey, static cd => cd.Span, rawData, cancellation);
}
/// <summary>
+ /// Gets an object from the server if it exists
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <typeparam name="TState"></typeparam>
+ /// <param name="cache"></param>
+ /// <param name="objectId">The id of the object to get</param>
+ /// <param name="cancellationToken">A token to cancel the operation</param>
+ /// <param name="getter">A callback function that computes an object result from binary data</param>
+ /// <param name="state">A user-state parameter to be passed back to the callback function</param>
+ /// <returns>A task that completes to return the results of the response payload</returns>
+ /// <exception cref="ArgumentException"></exception>
+ /// <exception cref="ArgumentNullException"></exception>
+ /// <exception cref="InvalidStatusException"></exception>
+ /// <exception cref="ObjectDisposedException"></exception>
+ /// <exception cref="InvalidResponseException"></exception>
+ public static async Task<T?> GetAsync<T, TState>(
+ this IGlobalCacheProvider cache,
+ string objectId,
+ GetObjectFromData<T, TState> getter,
+ TState state,
+ CancellationToken cancellationToken = default
+ )
+ {
+ ArgumentNullException.ThrowIfNull(cache);
+ ArgumentNullException.ThrowIfNull(getter);
+
+ //Get state will store the object result if successfull get operation
+ GetObjectState<T, TState> st = new(state, getter);
+
+ //Get the object, if successfull, compute the result
+ await cache.GetAsync(objectId, static (s, d) => s.ComputeResult(d), st, cancellationToken);
+
+ //If the get operation failed, return a default value
+ return st.Result;
+ }
+
+
+ /// <summary>
/// Asynchronously gets a value from the backing cache store
/// </summary>
/// <typeparam name="T"></typeparam>
@@ -89,6 +131,7 @@ namespace VNLib.Data.Caching
/// <returns>The value if found, or null if it does not exist in the store</returns>
public static Task<T?> GetAsync<T>(this IGlobalCacheProvider cache, string key, CancellationToken cancellation)
{
+ ArgumentNullException.ThrowIfNull(cache);
return cache.GetAsync<T>(key, cache.DefaultDeserializer, cancellation);
}
@@ -104,6 +147,7 @@ namespace VNLib.Data.Caching
/// <returns>A task that completes when the update operation has compelted</returns>
public static Task AddOrUpdateAsync<T>(this IGlobalCacheProvider cache, string key, string? newKey, T value, CancellationToken cancellation)
{
+ ArgumentNullException.ThrowIfNull(cache);
return cache.AddOrUpdateAsync(key, newKey, value, cache.DefaultSerializer, cancellation);
}
}
diff --git a/lib/VNLib.Data.Caching/src/IGlobalCacheProvider.cs b/lib/VNLib.Data.Caching/src/IGlobalCacheProvider.cs
index e04c9e4..1545b99 100644
--- a/lib/VNLib.Data.Caching/src/IGlobalCacheProvider.cs
+++ b/lib/VNLib.Data.Caching/src/IGlobalCacheProvider.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Data.Caching
@@ -23,6 +23,7 @@
*/
using System;
+using System.Buffers;
using System.Threading;
using System.Threading.Tasks;
@@ -39,12 +40,47 @@ namespace VNLib.Data.Caching
public delegate void ObjectDataSet<T>(T state, ReadOnlySpan<byte> objectData);
/// <summary>
- /// A delegate method that will get the raw objet data from a state object
+ /// A delegate method that will get the raw object data from a state object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="state">The state object passed to the caller</param>
/// <returns>The raw object data to store in cache</returns>
- public delegate ReadOnlySpan<byte> ObjectDataReader<T>(T state);
+ public delegate ReadOnlySpan<byte> ObjectDataGet<T>(T state);
+
+ /// <summary>
+ /// A delegate method that will write the raw object data to the supplied
+ /// data buffer
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="state">The state object passed to the caller</param>
+ /// <param name="finiteWriter">The finite sized buffer writer use to write object data to</param>
+ public delegate void ObjectDataReader<T>(T state, IBufferWriter<byte> finiteWriter);
+
+ /// <summary>
+ /// A delegate method that will get an object from the raw object data
+ /// </summary>
+ /// <typeparam name="TObject"></typeparam>
+ /// <typeparam name="TState"></typeparam>
+ /// <param name="state">Optional user-state data</param>
+ /// <param name="data">The object data to compute the object result from</param>
+ /// <returns>The resultant object</returns>
+ public delegate TObject GetObjectFromData<TObject, TState>(TState state, ReadOnlySpan<byte> data);
+
+ /// <summary>
+ /// Internal structure used to store a callback and state for the
+ /// a data read/get operation on a cache object
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="UserState">The user-state object to pass</param>
+ /// <param name="Getter">The data get callback function</param>
+ internal readonly record struct ObjectDataGetState<T>(T UserState, ObjectDataGet<T> Getter);
+
+ internal sealed class GetObjectState<T, TState>(TState State, GetObjectFromData<T, TState> Getter)
+ {
+ public T? Result;
+
+ public void ComputeResult(ReadOnlySpan<byte> data) => Result = Getter(State, data);
+ }
/// <summary>
/// A global cache provider interface
@@ -123,6 +159,6 @@ namespace VNLib.Data.Caching
/// <param name="state">The callback state parameter</param>
/// <param name="cancellation">A token to cancel the async operation</param>
/// <returns>A task that completes when the update operation has compelted</returns>
- Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation);
+ Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataGet<T> callback, T state, CancellationToken cancellation);
}
} \ No newline at end of file
diff --git a/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs b/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs
index 562c220..6b39580 100644
--- a/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs
+++ b/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Extensions.VNCache
@@ -164,12 +164,13 @@ namespace VNLib.Plugins.Extensions.VNCache.DataModel
/// <param name="factory">The factory callback function to produce a value when a cache miss occurs</param>
/// <param name="cancellation">A token to cancel the operation</param>
/// <returns>A task that completes by returning the entity</returns>
+ /// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public static async Task<T?> GetOrLoadAsync<T>(this IEntityCache<T> cache, string id, Func<string, CancellationToken, Task<T?>> factory, CancellationToken cancellation = default) where T : class
{
- _ = cache ?? throw new ArgumentNullException(nameof(cache));
- _ = id ?? throw new ArgumentNullException(nameof(id));
- _ = factory ?? throw new ArgumentNullException(nameof(factory));
+ ArgumentNullException.ThrowIfNull(cache);
+ ArgumentNullException.ThrowIfNull(factory);
+ ArgumentException.ThrowIfNullOrWhiteSpace(id);
//try to load the value from cache
T? record = await cache.GetAsync(id, cancellation);
@@ -241,7 +242,7 @@ namespace VNLib.Plugins.Extensions.VNCache.DataModel
///<inheritdoc/>
public override Task<bool> DeleteAsync(string key, CancellationToken cancellation)
{
- _ = key ?? throw new ArgumentNullException(nameof(key));
+ ArgumentException.ThrowIfNullOrWhiteSpace(key);
//Compute the key for the id
string scoped = KeyGen.ComputedKey(key);
return Cache.DeleteAsync(scoped, cancellation);
@@ -250,7 +251,7 @@ namespace VNLib.Plugins.Extensions.VNCache.DataModel
///<inheritdoc/>
public override Task<T> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
{
- _ = key ?? throw new ArgumentNullException(nameof(key));
+ ArgumentException.ThrowIfNullOrWhiteSpace(key);
//Compute the key for the id
string scoped = KeyGen.ComputedKey(key);
@@ -261,7 +262,7 @@ namespace VNLib.Plugins.Extensions.VNCache.DataModel
///<inheritdoc/>
public override Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
{
- _ = key ?? throw new ArgumentNullException(nameof(key));
+ ArgumentException.ThrowIfNullOrWhiteSpace(key);
//Compute primary key from id
string primary = KeyGen.ComputedKey(key);
@@ -275,7 +276,7 @@ namespace VNLib.Plugins.Extensions.VNCache.DataModel
///<inheritdoc/>
public override Task GetAsync<T>(string key, ObjectDataSet<T> callback, T state, CancellationToken cancellation)
{
- _ = key ?? throw new ArgumentNullException(nameof(key));
+ ArgumentException.ThrowIfNullOrWhiteSpace(key);
//Compute the key for the id
string scoped = KeyGen.ComputedKey(key);
@@ -284,9 +285,9 @@ namespace VNLib.Plugins.Extensions.VNCache.DataModel
}
///<inheritdoc/>
- public override Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation)
+ public override Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataGet<T> callback, T state, CancellationToken cancellation)
{
- _ = key ?? throw new ArgumentNullException(nameof(key));
+ ArgumentException.ThrowIfNullOrWhiteSpace(key);
//Compute primary key from id
string primary = KeyGen.ComputedKey(key);
diff --git a/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/ScopedCache.cs b/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/ScopedCache.cs
index 545e194..5107dea 100644
--- a/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/ScopedCache.cs
+++ b/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/ScopedCache.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Extensions.VNCache
@@ -65,7 +65,7 @@ namespace VNLib.Plugins.Extensions.VNCache.DataModel
public abstract Task GetAsync<T>(string key, ObjectDataSet<T> callback, T state, CancellationToken cancellation);
///<inheritdoc/>
- public abstract Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation);
+ public abstract Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataGet<T> callback, T state, CancellationToken cancellation);
///<inheritdoc/>
public abstract object GetUnderlyingStore();