Skip to content

Commit

Permalink
fixed #285
Browse files Browse the repository at this point in the history
  • Loading branch information
Valdis Iljuconoks committed May 5, 2024
1 parent a21851a commit 3714a7a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 31 deletions.
31 changes: 11 additions & 20 deletions src/DbLocalizationProvider/Cache/BaseCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@

namespace DbLocalizationProvider.Cache;

internal class BaseCacheManager : ICacheManager
internal class BaseCacheManager(ICacheManager inner) : ICacheManager
{
private readonly ConcurrentDictionary<string, object> _knownResourceKeys = new(StringComparer.InvariantCultureIgnoreCase);

private ICacheManager _inner;

public BaseCacheManager() { }

public BaseCacheManager(ICacheManager inner)
{
_inner = inner;
}

internal int KnownKeyCount => _knownResourceKeys.Count;

internal ICollection<string> KnownKeys => _knownResourceKeys.Keys;
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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.");
Expand Down
4 changes: 2 additions & 2 deletions src/DbLocalizationProvider/Cache/ICacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public interface ICacheManager
/// <summary>
/// Event raise is taken care by <see cref="BaseCacheManager" />.
/// </summary>
event CacheEventHandler OnInsert;
event CacheEventHandler? OnInsert;

/// <summary>
/// Event raise is taken care by <see cref="BaseCacheManager" />.
/// </summary>
event CacheEventHandler OnRemove;
event CacheEventHandler? OnRemove;
}
2 changes: 1 addition & 1 deletion src/DbLocalizationProvider/Commands/CreateNewResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Execute(Command command)

_repository.InsertResource(resource);

_configurationContext.Value.BaseCacheManager.StoreKnownKey(resource.ResourceKey);
_configurationContext.Value._baseCacheManager.StoreKnownKey(resource.ResourceKey);
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/DbLocalizationProvider/ConfigurationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ConfigurationContext
/// </summary>
public const string CultureForTranslationsFromCode = "";

internal readonly BaseCacheManager BaseCacheManager = new(new InMemoryCache());
internal BaseCacheManager _baseCacheManager = new(new InMemoryCache());

internal FallbackLanguagesCollection _fallbackCollection = new();

Expand Down Expand Up @@ -132,16 +132,21 @@ public ConfigurationContext(IServiceCollection services) : this()
/// </summary>
public ICacheManager CacheManager
{
get => BaseCacheManager;
get => _baseCacheManager;
set
{
if (value != null)
{
BaseCacheManager.SetInnerManager(value);
_baseCacheManager.SetInnerManager(value);
}
}
}

private void CopyCacheManager(BaseCacheManager cacheManager)
{
_baseCacheManager = cacheManager;
}

/// <summary>
/// Gets or sets flag to enable or disable invariant culture fallback (to use resource values discovered and registered from code).
/// Default <c>false</c>.
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/DbLocalizationProvider/Queries/GetTranslation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/DbLocalizationProvider/Sync/Synchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ private void StoreKnownResourcesAndPopulateCache(IEnumerable<LocalizationResourc
else
{
// just store resource cache keys
syncedResources.ForEach(r => _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<LocalizationResource> MergeLists(
Expand Down

0 comments on commit 3714a7a

Please sign in to comment.