From 9bb5ddd8f19c0ecabd7af4ee58d80c16826bc183 Mon Sep 17 00:00:00 2001 From: vman Date: Wed, 28 Dec 2022 14:19:32 -0500 Subject: Cache provider abstractions, reduced deps --- Plugins/CacheBroker/CacheBroker.csproj | 2 + .../Endpoints/BrokerRegistrationEndpoint.cs | 5 +- .../Endpoints/BrokerHeartBeat.cs | 6 +- .../Endpoints/ConnectEndpoint.cs | 14 +- .../SessionCacheServer/ObjectCacheServer.csproj | 11 +- .../SessionCacheServer/ObjectCacheServerEntry.cs | 6 +- Plugins/SessionProvider/GlobalCache.cs | 165 --------------------- Plugins/SessionProvider/SessionProvider.csproj | 2 + 8 files changed, 21 insertions(+), 190 deletions(-) delete mode 100644 Plugins/SessionProvider/GlobalCache.cs (limited to 'Plugins') diff --git a/Plugins/CacheBroker/CacheBroker.csproj b/Plugins/CacheBroker/CacheBroker.csproj index f4ea139..1d4fdcc 100644 --- a/Plugins/CacheBroker/CacheBroker.csproj +++ b/Plugins/CacheBroker/CacheBroker.csproj @@ -6,6 +6,8 @@ VNLib.Plugins.Cache.Broker Vaughn Nugent 1.0.1.2 + True + \\vaughnnugent.com\Internal\Folder Redirection\vman\Documents\Programming\Software\StrongNameingKey.snk diff --git a/Plugins/CacheBroker/Endpoints/BrokerRegistrationEndpoint.cs b/Plugins/CacheBroker/Endpoints/BrokerRegistrationEndpoint.cs index be700d1..7867e97 100644 --- a/Plugins/CacheBroker/Endpoints/BrokerRegistrationEndpoint.cs +++ b/Plugins/CacheBroker/Endpoints/BrokerRegistrationEndpoint.cs @@ -33,9 +33,7 @@ using System.Threading; using System.Net.Sockets; using System.Threading.Tasks; using System.Collections.Generic; -using System.Security.Cryptography; using System.Text.Json.Serialization; -using System.Security.Cryptography.X509Certificates; using RestSharp; @@ -110,8 +108,7 @@ namespace VNLib.Plugins.Cache.Broker.Endpoints private async Task GetClientPublic() { - using SecretResult secret = await this.GetPlugin().TryGetSecretAsync("client_public_key") ?? throw new InvalidOperationException("Client public key not found in vault"); - return secret.GetJsonWebKey(); + return await this.GetPlugin().TryGetSecretAsync("client_public_key").ToJsonWebKey() ?? throw new InvalidOperationException("Client public key not found in vault"); } private async Task GetCachePublic() diff --git a/Plugins/SessionCacheServer/Endpoints/BrokerHeartBeat.cs b/Plugins/SessionCacheServer/Endpoints/BrokerHeartBeat.cs index 2e380a3..bd1233e 100644 --- a/Plugins/SessionCacheServer/Endpoints/BrokerHeartBeat.cs +++ b/Plugins/SessionCacheServer/Endpoints/BrokerHeartBeat.cs @@ -37,7 +37,7 @@ using VNLib.Plugins.Extensions.Loading; namespace VNLib.Plugins.Essentials.Sessions.Server.Endpoints { - internal class BrokerHeartBeat : ResourceEndpointBase + internal sealed class BrokerHeartBeat : ResourceEndpointBase { public override string Path => "/heartbeat"; @@ -64,9 +64,7 @@ namespace VNLib.Plugins.Essentials.Sessions.Server.Endpoints private async Task GetBrokerPubAsync() { - using SecretResult brokerPubKey = await Pbase.TryGetSecretAsync("broker_public_key") ?? throw new KeyNotFoundException("Missing required secret : broker_public_key"); - - return brokerPubKey.GetJsonWebKey(); + return await Pbase.TryGetSecretAsync("broker_public_key").ToJsonWebKey() ?? throw new KeyNotFoundException("Missing required secret : broker_public_key"); } protected override async ValueTask GetAsync(HttpEntity entity) diff --git a/Plugins/SessionCacheServer/Endpoints/ConnectEndpoint.cs b/Plugins/SessionCacheServer/Endpoints/ConnectEndpoint.cs index 77acb13..2fe0994 100644 --- a/Plugins/SessionCacheServer/Endpoints/ConnectEndpoint.cs +++ b/Plugins/SessionCacheServer/Endpoints/ConnectEndpoint.cs @@ -47,7 +47,7 @@ using VNLib.Plugins.Essentials.Extensions; namespace VNLib.Plugins.Essentials.Sessions.Server.Endpoints { - class ConnectEndpoint : ResourceEndpointBase + internal sealed class ConnectEndpoint : ResourceEndpointBase { const int MAX_RECV_BUF_SIZE = 1000 * 1024; const int MIN_RECV_BUF_SIZE = 8 * 1024; @@ -194,21 +194,15 @@ namespace VNLib.Plugins.Essentials.Sessions.Server.Endpoints private async Task GetClientPubAsync() { - using SecretResult brokerPubKey = await Pbase.TryGetSecretAsync("client_public_key") ?? throw new KeyNotFoundException("Missing required secret : client_public_key"); - - return brokerPubKey.GetJsonWebKey(); + return await Pbase.TryGetSecretAsync("client_public_key").ToJsonWebKey() ?? throw new KeyNotFoundException("Missing required secret : client_public_key"); } private async Task GetCachePubAsync() { - using SecretResult cachPublic = await Pbase.TryGetSecretAsync("cache_public_key") ?? throw new KeyNotFoundException("Missing required secret : client_public_key"); - - return cachPublic.GetJsonWebKey(); + return await Pbase.TryGetSecretAsync("cache_public_key").ToJsonWebKey() ?? throw new KeyNotFoundException("Missing required secret : client_public_key"); } private async Task GetCachePrivateKeyAsync() { - using SecretResult cachePrivate = await Pbase.TryGetSecretAsync("cache_private_key") ?? throw new KeyNotFoundException("Missing required secret : client_public_key"); - - return cachePrivate.GetJsonWebKey(); + return await Pbase.TryGetSecretAsync("cache_private_key").ToJsonWebKey() ?? throw new KeyNotFoundException("Missing required secret : client_public_key"); } private async Task ChangeWorkerAsync(CancellationToken cancellation) diff --git a/Plugins/SessionCacheServer/ObjectCacheServer.csproj b/Plugins/SessionCacheServer/ObjectCacheServer.csproj index 2cf298d..ff239cc 100644 --- a/Plugins/SessionCacheServer/ObjectCacheServer.csproj +++ b/Plugins/SessionCacheServer/ObjectCacheServer.csproj @@ -1,5 +1,4 @@ - net6.0 enable @@ -7,7 +6,8 @@ 1.0.1.1 VNLib.Plugins.Essentials.Sessions.Server Copyright © 2022 Vaughn Nugent - + True + \\vaughnnugent.com\Internal\Folder Redirection\vman\Documents\Programming\Software\StrongNameingKey.snk @@ -52,7 +52,12 @@ - + + + + + + diff --git a/Plugins/SessionCacheServer/ObjectCacheServerEntry.cs b/Plugins/SessionCacheServer/ObjectCacheServerEntry.cs index 20a6268..85a7996 100644 --- a/Plugins/SessionCacheServer/ObjectCacheServerEntry.cs +++ b/Plugins/SessionCacheServer/ObjectCacheServerEntry.cs @@ -287,14 +287,12 @@ namespace VNLib.Plugins.Essentials.Sessions.Server private async Task GetCachePrivate() { - using SecretResult secret = await this.TryGetSecretAsync("cache_private_key") ?? throw new KeyNotFoundException("Failed to load the cache private key"); - return secret.GetJsonWebKey(); + return await this.TryGetSecretAsync("cache_private_key").ToJsonWebKey() ?? throw new KeyNotFoundException("Failed to load the cache private key"); } private async Task GetBrokerPublic() { - using SecretResult secret = await this.TryGetSecretAsync("broker_public_key") ?? throw new KeyNotFoundException("Failed to load the broker's public key"); - return secret.GetJsonWebKey(); + return await this.TryGetSecretAsync("broker_public_key").ToJsonWebKey() ?? throw new KeyNotFoundException("Failed to load the broker's public key"); } diff --git a/Plugins/SessionProvider/GlobalCache.cs b/Plugins/SessionProvider/GlobalCache.cs deleted file mode 100644 index 2f64632..0000000 --- a/Plugins/SessionProvider/GlobalCache.cs +++ /dev/null @@ -1,165 +0,0 @@ -/* -* Copyright (c) 2022 Vaughn Nugent -* -* Library: VNLib -* Package: SessionProvider -* File: GlobalCache.cs -* -* GlobalCache.cs is part of SessionProvider which is part of the larger -* VNLib collection of libraries and utilities. -* -* SessionProvider is free software: you can redistribute it and/or modify -* it under the terms of the GNU Affero General Public License as -* published by the Free Software Foundation, either version 3 of the -* License, or (at your option) any later version. -* -* SessionProvider is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Affero General Public License for more details. -* -* You should have received a copy of the GNU Affero General Public License -* along with this program. If not, see https://www.gnu.org/licenses/. -*/ - -using System; -using System.Threading; -using System.Threading.Tasks; - -//using VNLib.Data.Caching; -//using VNLib.Net.Messaging.FBM.Client; -//using VNLib.Net.Messaging.FBM.Client.Exceptions; - - -namespace VNLib.Plugins.Essentials.Sessions -{ - /*internal class GlobalCache : IGlobalCacheProvider - { - private readonly FBMClient Client; - private readonly TimeSpan OperationTimeout; - - public GlobalCache(FBMClient cacheProvider, TimeSpan cancellation) - { - this.Client = cacheProvider; - this.OperationTimeout = cancellation; - } - - //If the wait handle will block, the client is connected - bool IGlobalCacheProvider.IsConnected => !Client.ConnectionStatusHandle.WaitOne(0); - - async Task IGlobalCacheProvider.DeleteAsync(string key) - { - if (OperationTimeout > TimeSpan.Zero && OperationTimeout < TimeSpan.MaxValue) - { - CancellationTokenSource cts = new(OperationTimeout); - try - { - //Delete value - await Client.DeleteObjectAsync(key, cts.Token); - } - catch (FBMException fbm) - { - //Catch fbm excpetions and wrap them in global cache exception - throw new GlobalCacheException("Failed to delete cache record, see inner exception", fbm); - } - catch (OperationCanceledException) - { - throw new TimeoutException("The operation has been cancelled, due to a timeout"); - } - finally - { - cts.Dispose(); - } - } - else - { - try - { - //Delete value - await Client.DeleteObjectAsync(key); - } - catch (FBMException fbm) - { - //Catch fbm excpetions and wrap them in global cache exception - throw new GlobalCacheException("Failed to delete cache record, see inner exception", fbm); - } - } - } - - async Task IGlobalCacheProvider.GetAsync(string key) - { - if (OperationTimeout > TimeSpan.Zero && OperationTimeout < TimeSpan.MaxValue) - { - CancellationTokenSource cts = new(OperationTimeout); - try - { - //Try to get the value - return await Client.GetObjectAsync(key, cts.Token); - } - catch (FBMException fbm) - { - //Catch fbm excpetions and wrap them in global cache exception - throw new GlobalCacheException("Failed to delete cache record, see inner exception", fbm); - } - catch (OperationCanceledException) - { - throw new TimeoutException("The operation has been cancelled, due to a timeout"); - } - finally - { - cts.Dispose(); - } - } - else - { - try - { - //Try to get the value - return await Client.GetObjectAsync(key); - } - catch (FBMException fbm) - { - //Catch fbm excpetions and wrap them in global cache exception - throw new GlobalCacheException("Failed to delete cache record, see inner exception", fbm); - } - } - } - - async Task IGlobalCacheProvider.SetAsync(string key, T value) - { - if (OperationTimeout > TimeSpan.Zero && OperationTimeout < TimeSpan.MaxValue) - { - CancellationTokenSource cts = new(OperationTimeout); - try - { - await Client.AddOrUpdateObjectAsync(key, null, value, cts.Token); - } - catch (FBMException fbm) - { - //Catch fbm excpetions and wrap them in global cache exception - throw new GlobalCacheException("Failed to delete cache record, see inner exception", fbm); - } - catch (OperationCanceledException) - { - throw new TimeoutException("The operation has been cancelled, due to a timeout"); - } - finally - { - cts.Dispose(); - } - } - else - { - try - { - await Client.AddOrUpdateObjectAsync(key, null, value); - } - catch (FBMException fbm) - { - //Catch fbm excpetions and wrap them in global cache exception - throw new GlobalCacheException("Failed to delete cache record, see inner exception", fbm); - } - } - } - }*/ -} diff --git a/Plugins/SessionProvider/SessionProvider.csproj b/Plugins/SessionProvider/SessionProvider.csproj index 98fb5da..6401cb9 100644 --- a/Plugins/SessionProvider/SessionProvider.csproj +++ b/Plugins/SessionProvider/SessionProvider.csproj @@ -11,6 +11,8 @@ Copyright © 2022 Vaughn Nugent 1.0.3.1 https://www.vaughnnugent.com/resources + True + \\vaughnnugent.com\Internal\Folder Redirection\vman\Documents\Programming\Software\StrongNameingKey.snk -- cgit