Skip to content

Commit

Permalink
added caching on unknown resources
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisiljuconoks committed Mar 2, 2022
1 parent 07df45f commit 6b11f1e
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/DbLocalizationProvider/Queries/GetTranslation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,46 +52,25 @@ public string Execute(Query query)
}

var key = query.Key;
var cacheKey = CacheKeyHelper.BuildKey(key);

if (_configurationContext.BaseCacheManager.AreKnownKeysStored() && !_configurationContext.BaseCacheManager.IsKeyKnown(key))
{
// we are here couple of reasons:
// we are here because of 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)
//
// this means that we can try to lookup resource once more in database and if not found - then we can short-break the circuit

var lastAttemptResource = GetResourceFromDb(key);
if (lastAttemptResource != null)
{
_configurationContext.CacheManager.Insert(cacheKey, lastAttemptResource, true);
}
else
{
return null;
}
// if this resource is not yet found in cache
// we can try to lookup resource once more in database and if not found - then we can short-break the circuit

GetCachedResourceOrReadFromStorage(query);
}

if (_configurationContext.DiagnosticsEnabled)
{
_logger.Debug($"Executing query for resource key `{key}` (lang: `{query.Language.Name})..");
}

var localizationResource = _configurationContext.CacheManager.Get(cacheKey) as LocalizationResource;

if (localizationResource == null)
{
if (_configurationContext.DiagnosticsEnabled)
{
_logger.Info(
$"MISSING: Resource Key (culture: {query.Language.Name}): {key}. Probably class is not decorated with either [LocalizedModel] or [LocalizedResource] attribute.");
}

// resource is not found in the cache, let's check database
localizationResource = GetResourceFromDb(key) ?? LocalizationResource.CreateNonExisting(key);
_configurationContext.CacheManager.Insert(cacheKey, localizationResource, true);
}
var localizationResource = GetCachedResourceOrReadFromStorage(query);

return query.FallbackToInvariant
? localizationResource.Translations.ByLanguage(query.Language, true)
Expand All @@ -111,6 +90,29 @@ protected virtual LocalizationResource GetResourceFromDb(string key)

return _queryExecutor.Execute(q);
}

private LocalizationResource GetCachedResourceOrReadFromStorage(Query query)
{
var key = query.Key;
var cacheKey = CacheKeyHelper.BuildKey(key);

if (_configurationContext.CacheManager.Get(cacheKey) is LocalizationResource localizationResource)
{
return localizationResource;
}

if (_configurationContext.DiagnosticsEnabled)
{
_logger.Info(
$"MISSING: Resource Key (culture: {query.Language.Name}): {key}. Probably class is not decorated with either [LocalizedModel] or [LocalizedResource] attribute.");
}

// resource is not found in the cache, let's check database
localizationResource = GetResourceFromDb(key) ?? LocalizationResource.CreateNonExisting(key);
_configurationContext.CacheManager.Insert(cacheKey, localizationResource, true);

return localizationResource;
}
}

/// <summary>
Expand Down

0 comments on commit 6b11f1e

Please sign in to comment.