From 3714a7a7b99fd207ddf084ab217acb40924351b8 Mon Sep 17 00:00:00 2001 From: Valdis Iljuconoks Date: Sun, 5 May 2024 20:32:31 +0300 Subject: [PATCH] fixed https://github.com/valdisiljuconoks/LocalizationProvider/issues/285 --- .../Cache/BaseCacheManager.cs | 31 +++++++------------ .../Cache/ICacheManager.cs | 4 +-- .../Commands/CreateNewResources.cs | 2 +- .../ConfigurationContext.cs | 13 ++++++-- .../Queries/GetTranslation.cs | 12 +++++-- .../Sync/Synchronizer.cs | 4 +-- 6 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/DbLocalizationProvider/Cache/BaseCacheManager.cs b/src/DbLocalizationProvider/Cache/BaseCacheManager.cs index 6b9292fa..8e0a504c 100644 --- a/src/DbLocalizationProvider/Cache/BaseCacheManager.cs +++ b/src/DbLocalizationProvider/Cache/BaseCacheManager.cs @@ -7,19 +7,10 @@ namespace DbLocalizationProvider.Cache; -internal class BaseCacheManager : ICacheManager +internal class BaseCacheManager(ICacheManager inner) : ICacheManager { private readonly ConcurrentDictionary _knownResourceKeys = new(StringComparer.InvariantCultureIgnoreCase); - private ICacheManager _inner; - - public BaseCacheManager() { } - - public BaseCacheManager(ICacheManager inner) - { - _inner = inner; - } - internal int KnownKeyCount => _knownResourceKeys.Count; internal ICollection KnownKeys => _knownResourceKeys.Keys; @@ -28,7 +19,7 @@ public void Insert(string key, object value, bool insertIntoKnownResourceKeys) { VerifyInstance(); - _inner.Insert(key.ToLower(), value, insertIntoKnownResourceKeys); + inner.Insert(key.ToLower(), value, insertIntoKnownResourceKeys); var resourceKey = CacheKeyHelper.GetResourceKeyFromCacheKey(key); if (insertIntoKnownResourceKeys) @@ -42,23 +33,23 @@ public void Insert(string key, object value, bool insertIntoKnownResourceKeys) public object Get(string key) { VerifyInstance(); - return _inner.Get(key.ToLower()); + return inner.Get(key.ToLower()); } public void Remove(string key) { VerifyInstance(); - _inner.Remove(key.ToLower()); + inner.Remove(key.ToLower()); OnRemove?.Invoke(new CacheEventArgs(CacheOperation.Remove, key, CacheKeyHelper.GetResourceKeyFromCacheKey(key))); } - public event CacheEventHandler OnInsert; - public event CacheEventHandler OnRemove; + public event CacheEventHandler? OnInsert; + public event CacheEventHandler? OnRemove; - public void SetInnerManager(ICacheManager inner) + public void SetInnerManager(ICacheManager implementation) { - _inner = inner ?? throw new ArgumentNullException(nameof(inner)); + inner = implementation ?? throw new ArgumentNullException(nameof(implementation)); } internal bool IsKeyKnown(string key) @@ -73,17 +64,17 @@ internal void StoreKnownKey(string key) internal void SetKnownKeysStored() { - _inner.Insert(CacheKeyHelper.BuildKey("KnownKeysSynched"), true, false); + inner.Insert(CacheKeyHelper.BuildKey("KnownKeysSynched"), true, false); } internal bool AreKnownKeysStored() { - return _inner.Get(CacheKeyHelper.BuildKey("KnownKeysSynched")) as bool? ?? false; + return inner.Get(CacheKeyHelper.BuildKey("KnownKeysSynched")) as bool? ?? false; } private void VerifyInstance() { - if (_inner == null) + if (inner == null) { throw new InvalidOperationException( "Cache implementation is not set. Use `ConfigurationContext.CacheManager` setter."); diff --git a/src/DbLocalizationProvider/Cache/ICacheManager.cs b/src/DbLocalizationProvider/Cache/ICacheManager.cs index a088453e..18492200 100644 --- a/src/DbLocalizationProvider/Cache/ICacheManager.cs +++ b/src/DbLocalizationProvider/Cache/ICacheManager.cs @@ -32,10 +32,10 @@ public interface ICacheManager /// /// Event raise is taken care by . /// - event CacheEventHandler OnInsert; + event CacheEventHandler? OnInsert; /// /// Event raise is taken care by . /// - event CacheEventHandler OnRemove; + event CacheEventHandler? OnRemove; } diff --git a/src/DbLocalizationProvider/Commands/CreateNewResources.cs b/src/DbLocalizationProvider/Commands/CreateNewResources.cs index 76e08542..72b894fb 100644 --- a/src/DbLocalizationProvider/Commands/CreateNewResources.cs +++ b/src/DbLocalizationProvider/Commands/CreateNewResources.cs @@ -73,7 +73,7 @@ public void Execute(Command command) _repository.InsertResource(resource); - _configurationContext.Value.BaseCacheManager.StoreKnownKey(resource.ResourceKey); + _configurationContext.Value._baseCacheManager.StoreKnownKey(resource.ResourceKey); } } } diff --git a/src/DbLocalizationProvider/ConfigurationContext.cs b/src/DbLocalizationProvider/ConfigurationContext.cs index d1850ffa..3b359bb2 100644 --- a/src/DbLocalizationProvider/ConfigurationContext.cs +++ b/src/DbLocalizationProvider/ConfigurationContext.cs @@ -28,7 +28,7 @@ public class ConfigurationContext /// public const string CultureForTranslationsFromCode = ""; - internal readonly BaseCacheManager BaseCacheManager = new(new InMemoryCache()); + internal BaseCacheManager _baseCacheManager = new(new InMemoryCache()); internal FallbackLanguagesCollection _fallbackCollection = new(); @@ -132,16 +132,21 @@ public ConfigurationContext(IServiceCollection services) : this() /// public ICacheManager CacheManager { - get => BaseCacheManager; + get => _baseCacheManager; set { if (value != null) { - BaseCacheManager.SetInnerManager(value); + _baseCacheManager.SetInnerManager(value); } } } + private void CopyCacheManager(BaseCacheManager cacheManager) + { + _baseCacheManager = cacheManager; + } + /// /// Gets or sets flag to enable or disable invariant culture fallback (to use resource values discovered and registered from code). /// Default false. @@ -230,6 +235,8 @@ internal void CopyFrom(ConfigurationContext ctx) throw new ArgumentNullException(nameof(ctx)); } + CopyCacheManager(ctx._baseCacheManager); + EnableLocalization = ctx.EnableLocalization; EnableLegacyMode = ctx.EnableLegacyMode; DiscoverAndRegisterResources = ctx.DiscoverAndRegisterResources; diff --git a/src/DbLocalizationProvider/Queries/GetTranslation.cs b/src/DbLocalizationProvider/Queries/GetTranslation.cs index 08af85d6..59731377 100644 --- a/src/DbLocalizationProvider/Queries/GetTranslation.cs +++ b/src/DbLocalizationProvider/Queries/GetTranslation.cs @@ -54,10 +54,10 @@ public string Execute(Query query) var key = query.Key; - if (_configurationContext.Value.BaseCacheManager.AreKnownKeysStored() - && !_configurationContext.Value.BaseCacheManager.IsKeyKnown(key)) + if (_configurationContext.Value._baseCacheManager.AreKnownKeysStored() + && !_configurationContext.Value._baseCacheManager.IsKeyKnown(key)) { - // we are here because of couple of reasons: + // we are here because of a couple of reasons: // * someone is asking for non-existing resource (known keys are synced and key does not exist) // * someone has programmatically created resource and query is made on different cluster node (cache is still cold for this resource) // @@ -73,6 +73,12 @@ public string Execute(Query query) } var localizationResource = GetCachedResourceOrReadFromStorage(query); + + // if there are translation, we can cut short and return immediately + if (localizationResource.Translations.Count == 0) + { + return null; + } return query.FallbackToInvariant ? localizationResource.Translations.ByLanguage(query.Language, true) diff --git a/src/DbLocalizationProvider/Sync/Synchronizer.cs b/src/DbLocalizationProvider/Sync/Synchronizer.cs index 7ac9aba6..ce358dc1 100644 --- a/src/DbLocalizationProvider/Sync/Synchronizer.cs +++ b/src/DbLocalizationProvider/Sync/Synchronizer.cs @@ -207,10 +207,10 @@ private void StoreKnownResourcesAndPopulateCache(IEnumerable _configurationContext.Value.BaseCacheManager.StoreKnownKey(r.ResourceKey)); + syncedResources.ForEach(r => _configurationContext.Value._baseCacheManager.StoreKnownKey(r.ResourceKey)); } - _configurationContext.Value.BaseCacheManager.SetKnownKeysStored(); + _configurationContext.Value._baseCacheManager.SetKnownKeysStored(); } internal IEnumerable MergeLists(