diff options
Diffstat (limited to 'lib/VNLib.Plugins.Extensions.VNCache/src/VNCacheExtensions.cs')
-rw-r--r-- | lib/VNLib.Plugins.Extensions.VNCache/src/VNCacheExtensions.cs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/lib/VNLib.Plugins.Extensions.VNCache/src/VNCacheExtensions.cs b/lib/VNLib.Plugins.Extensions.VNCache/src/VNCacheExtensions.cs new file mode 100644 index 0000000..5f58142 --- /dev/null +++ b/lib/VNLib.Plugins.Extensions.VNCache/src/VNCacheExtensions.cs @@ -0,0 +1,108 @@ +/* +* Copyright (c) 2022 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Plugins.Extensions.VNCache +* File: VNCacheExtensions.cs +* +* VNCacheExtensions.cs is part of VNLib.Plugins.Extensions.VNCache which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Plugins.Extensions.VNCache 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. +* +* VNLib.Plugins.Extensions.VNCache 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.Text.Json; + +using VNLib.Utils.Logging; +using VNLib.Data.Caching; +using VNLib.Data.Caching.Extensions; +using VNLib.Plugins.Extensions.Loading; + +namespace VNLib.Plugins.Extensions.VNCache +{ + /// <summary> + /// Contains extension methods for aquiring a Plugin managed + /// global cache provider. + /// </summary> + public static class VNCacheExtensions + { + /// <summary> + /// Loads the shared cache provider for the current plugin + /// </summary> + /// <param name="pbase"></param> + /// <param name="localized">A localized log provider to write cache logging information to</param> + /// <returns>The shared <see cref="IGlobalCacheProvider"/> </returns> + /// <remarks> + /// The returned instance, background work, logging, and its lifetime + /// are managed by the current plugin. Beware when calling this method + /// network connections may be spawend and managed in the background by + /// this library. + /// </remarks> + public static VnCacheClient GetGlobalCache(this PluginBase pbase, ILogProvider? localized = null) + => LoadingExtensions.GetOrCreateSingleton<VnCacheClient>(pbase, localized == null ? LoadCacheClient : (pbase) => LoadCacheClient(pbase, localized)); + + private static VnCacheClient LoadCacheClient(PluginBase pbase) => LoadCacheClient(pbase, pbase.Log); + + private static VnCacheClient LoadCacheClient(PluginBase pbase, ILogProvider localized) + { + //Get config for client + IReadOnlyDictionary<string, JsonElement> config = pbase.GetConfigForType<VnCacheClient>(); + + //Init client + ILogProvider? debugLog = pbase.IsDebug() ? pbase.Log : null; + VnCacheClient client = new(debugLog); + + //Begin cache connections by scheduling a task on the plugin's scheduler + _ = pbase.DeferTask(() => RunClientAsync(pbase, config, localized, client), 250); + + return client; + } + + private static async Task RunClientAsync(PluginBase pbase, IReadOnlyDictionary<string, JsonElement> config, ILogProvider localized, VnCacheClient client) + { + ILogProvider Log = localized; + + try + { + //Try loading config + await client.LoadConfigAsync(pbase, config); + + Log.Verbose("VNCache client configration loaded successfully"); + + //Run and wait for exit + await client.RunAsync(Log, pbase.UnloadToken); + } + catch (OperationCanceledException) + { } + catch (KeyNotFoundException e) + { + Log.Error("Missing required configuration variable for VnCache client: {0}", e.Message); + } + catch (FBMServerNegiationException fne) + { + Log.Error("Failed to negotiate connection with cache server {reason}", fne.Message); + } + catch (Exception ex) + { + Log.Error(ex, "Unhandled exception occured in background cache client listening task"); + } + finally + { + client.Dispose(); + } + + Log.Information("Cache client exited"); + } + } +} |