diff options
Diffstat (limited to 'plugins/VNLib.Data.Caching.Providers.VNCache')
4 files changed, 65 insertions, 25 deletions
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs index 07fc9ee..e84a077 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs @@ -78,7 +78,8 @@ namespace VNLib.Data.Caching.Providers.VNCache public FBMCacheClient(PluginBase plugin, IConfigScope config) : this( config.Deserialze<VnCacheClientConfig>(), - plugin.IsDebug() ? plugin.Log : null + plugin.IsDebug() ? plugin.Log : null, + plugin ) { ILogProvider scoped = plugin.Log.CreateScope(LOG_NAME); @@ -103,7 +104,11 @@ namespace VNLib.Data.Caching.Providers.VNCache } } - public FBMCacheClient(VnCacheClientConfig config, ILogProvider? debugLog):base(config) + public FBMCacheClient(VnCacheClientConfig config, ILogProvider? debugLog) : this(config, debugLog, null) + { } + + + private FBMCacheClient(VnCacheClientConfig config, ILogProvider? debugLog, PluginBase? plugin) : base(config) { //Validate config (config as IOnConfigValidation).Validate(); @@ -115,13 +120,13 @@ namespace VNLib.Data.Caching.Providers.VNCache //Init the client with default settings FBMClientConfig conf = FBMDataCacheExtensions.GetDefaultConfig(BufferHeap, (int)config.MaxBlobSize, config.RequestTimeout, debugLog); - + FBMClientFactory clientFactory = new( - in conf, - new FBMFallbackClientWsFactory(), + in conf, + new FBMFallbackClientWsFactory(), 10 ); - + _cluster = (new CacheClientConfiguration()) .WithTls(config.UseTls) .WithInitialPeers(config.GetInitialNodeUris()) @@ -129,6 +134,9 @@ namespace VNLib.Data.Caching.Providers.VNCache //Init index _index = ClusterNodeIndex.CreateIndex(_cluster); + + //Init serializers + InitSerializers(config, plugin); } /* @@ -296,7 +304,7 @@ namespace VNLib.Data.Caching.Providers.VNCache public override Task<bool> DeleteAsync(string key, CancellationToken cancellation) { return !IsConnected - ? throw new InvalidOperationException("The underlying client is not connected to a cache node") + ? Task.FromException<bool>(new InvalidOperationException("The underlying client is not connected to a cache node")) : _client!.DeleteObjectAsync(key, cancellation); } @@ -304,7 +312,7 @@ namespace VNLib.Data.Caching.Providers.VNCache public override Task<T> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation) { return !IsConnected - ? throw new InvalidOperationException("The underlying client is not connected to a cache node") + ? Task.FromException<T>(new InvalidOperationException("The underlying client is not connected to a cache node")) : _client!.GetObjectAsync<T>(key, deserializer, cancellation); } @@ -312,7 +320,7 @@ namespace VNLib.Data.Caching.Providers.VNCache public override Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation) { return !IsConnected - ? throw new InvalidOperationException("The underlying client is not connected to a cache node") + ? Task.FromException(new InvalidOperationException("The underlying client is not connected to a cache node")) : _client!.AddOrUpdateObjectAsync(key, newKey, value, serialzer, cancellation); } @@ -320,7 +328,7 @@ namespace VNLib.Data.Caching.Providers.VNCache public override Task GetAsync<T>(string key, ObjectDataSet<T> callback, T state, CancellationToken cancellation) { return !IsConnected - ? throw new InvalidOperationException("The underlying client is not connected to a cache node") + ? Task.FromException(new InvalidOperationException("The underlying client is not connected to a cache node")) : _client!.GetObjectAsync(key, callback, state, cancellation); } @@ -328,7 +336,7 @@ namespace VNLib.Data.Caching.Providers.VNCache public override Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataGet<T> callback, T state, CancellationToken cancellation) { return !IsConnected - ? throw new InvalidOperationException("The underlying client is not connected to a cache node") + ? Task.FromException(new InvalidOperationException("The underlying client is not connected to a cache node")) : _client!.AddOrUpdateObjectAsync(key, newKey, callback, state, cancellation); } diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheBase.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheBase.cs index c337ef4..dc1ab8f 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheBase.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheBase.cs @@ -25,26 +25,22 @@ using System.Threading; using System.Threading.Tasks; +using VNLib.Plugins; +using VNLib.Plugins.Extensions.Loading; + namespace VNLib.Data.Caching.Providers.VNCache { - internal abstract class VNCacheBase : IGlobalCacheProvider + internal abstract class VNCacheBase(VNCacheConfig conf) : IGlobalCacheProvider { ///<inheritdoc/> public abstract bool IsConnected { get; } ///<inheritdoc/> - public virtual ICacheObjectDeserializer DefaultDeserializer { get; } + public virtual ICacheObjectDeserializer DefaultDeserializer => conf.CacheObjectDeserializer!; ///<inheritdoc/> - public virtual ICacheObjectSerializer DefaultSerializer { get; } - - protected VNCacheBase(VNCacheConfig config) - { - //Set default serializers - DefaultDeserializer = config.CacheObjectDeserializer ?? new JsonCacheObjectSerializer(256); - DefaultSerializer = config.CacheObjectSerializer ?? new JsonCacheObjectSerializer(256); - } - + public virtual ICacheObjectSerializer DefaultSerializer => conf.CacheObjectSerializer!; + ///<inheritdoc/> public abstract Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation); @@ -62,5 +58,36 @@ namespace VNLib.Data.Caching.Providers.VNCache ///<inheritdoc/> public abstract object GetUnderlyingStore(); + + /// <summary> + /// Initializes a set of cache object serializers and deserializers + /// for the configuration instance and loads external serializers if specified + /// by the user. + /// </summary> + /// <param name="config">The configuration instance to initialzie</param> + /// <param name="plugin">Optional plugin for loading external serializers</param> + protected static void InitSerializers(VNCacheConfig config, PluginBase? plugin) + { + //See if user has specified a custom serializer assembly + if (!string.IsNullOrWhiteSpace(config.SerializerDllPath)) + { + //Load the custom serializer assembly and get the serializer and deserializer instances + config.CacheObjectSerializer = plugin.CreateServiceExternal<ICacheObjectSerializer>(config.SerializerDllPath); + + //Avoid creating another instance if the deserializer is the same as the serializer + if(config.CacheObjectSerializer is ICacheObjectDeserializer cod) + { + config.CacheObjectDeserializer = cod; + } + else + { + config.CacheObjectDeserializer = plugin.CreateServiceExternal<ICacheObjectDeserializer>(config.SerializerDllPath); + } + } + + //If no default serializer is set, use the default JSON serializer + config.CacheObjectSerializer ??= new JsonCacheObjectSerializer(256); + config.CacheObjectDeserializer ??= new JsonCacheObjectSerializer(256); + } } }
\ No newline at end of file diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs index 8311519..24008b3 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Data.Caching.Providers.VNCache @@ -98,6 +98,11 @@ namespace VNLib.Data.Caching.Providers.VNCache throw new ArgumentException("You must configure a maximum object size", "max_object_size"); } } - + + /// <summary> + /// Optional external cache serializer library to load + /// </summary> + [JsonPropertyName("serializer_assebly_name")] + public string? SerializerDllPath { get; set; } } }
\ No newline at end of file diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNLib.Data.Caching.Providers.VNCache.csproj b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNLib.Data.Caching.Providers.VNCache.csproj index 99a5962..ded805a 100644 --- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNLib.Data.Caching.Providers.VNCache.csproj +++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNLib.Data.Caching.Providers.VNCache.csproj @@ -39,7 +39,7 @@ </ItemGroup> <ItemGroup> - <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Loading\src\VNLib.Plugins.Extensions.Loading.csproj" /> + <ProjectReference Include="..\..\..\..\VNLib.Plugins.Extensions\lib\VNLib.Plugins.Extensions.Loading\src\VNLib.Plugins.Extensions.Loading.csproj" /> <ProjectReference Include="..\..\..\lib\VNLib.Data.Caching.Extensions\src\VNLib.Data.Caching.Extensions.csproj" /> <ProjectReference Include="..\..\..\lib\VNLib.Data.Caching.ObjectCache\src\VNLib.Data.Caching.ObjectCache.csproj" /> <ProjectReference Include="..\..\..\lib\VNLib.Data.Caching\src\VNLib.Data.Caching.csproj" /> |