From 7587acbd26680cf444d1f9676b6473f6c71f340c Mon Sep 17 00:00:00 2001 From: Allan Ritchie Date: Sat, 29 Jun 2024 12:37:26 -0400 Subject: [PATCH] Update StorageManager.cs --- .../Infrastructure/Impl/StorageManager.cs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Shiny.Mediator.Maui/Infrastructure/Impl/StorageManager.cs b/src/Shiny.Mediator.Maui/Infrastructure/Impl/StorageManager.cs index f15dedc..de37b68 100644 --- a/src/Shiny.Mediator.Maui/Infrastructure/Impl/StorageManager.cs +++ b/src/Shiny.Mediator.Maui/Infrastructure/Impl/StorageManager.cs @@ -10,7 +10,7 @@ public class StorageManager(IFileSystem fileSystem) : IStorageManager Dictionary keys = null!; - public void Store(object request, object result, bool isPersistent) + public virtual void Store(object request, object result, bool isPersistent) { if (isPersistent) { @@ -27,7 +27,7 @@ public void Store(object request, object result, bool isPersistent) } - public TResult? Get(object request, bool isPersistent) + public virtual TResult? Get(object request, bool isPersistent) { TResult? returnValue = default; @@ -55,8 +55,10 @@ public void Store(object request, object result, bool isPersistent) } return returnValue; } + - public void ClearAll() + // TODO: clear by category (cache/replay/offline) + public virtual void ClearAll() { lock (this.memCache) this.memCache.Clear(); @@ -69,7 +71,7 @@ public void ClearAll() } - string GetStoreKeyFromRequest(object request) + protected virtual string GetStoreKeyFromRequest(object request) { if (request is IRequestKey keyProvider) return keyProvider.GetKey(); @@ -111,12 +113,12 @@ protected virtual string GetFilePath(object request, bool createIfNotExists) bool initialized = false; - void EnsureKeyLoad() + protected void EnsureKeyLoad() { if (this.initialized) return; - var storePath = Path.Combine(fileSystem.AppDataDirectory, "keys.mediator"); + var storePath = this.KeyStorePath; if (File.Exists(storePath)) { var json = File.ReadAllText(storePath); @@ -130,10 +132,12 @@ void EnsureKeyLoad() } - void PersistKeyStore() + protected void PersistKeyStore() { - var storePath = Path.Combine(fileSystem.AppDataDirectory, "keys.mediator"); var json = JsonSerializer.Serialize(this.keys); - File.WriteAllText(storePath, json); + File.WriteAllText(this.KeyStorePath, json); } + + + protected string KeyStorePath => Path.Combine(fileSystem.AppDataDirectory, "keys.mediator"); } \ No newline at end of file