-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
20 changed files
with
235 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
DbLocalizationProvider/CachedLocalizationResourceRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Web; | ||
using EPiServer; | ||
using EPiServer.Framework.Localization; | ||
|
||
namespace DbLocalizationProvider | ||
{ | ||
public class CachedLocalizationResourceRepository : ILocalizationResourceRepository | ||
{ | ||
private const string CacheKeyPrefix = "DbLocalizationProviderCache"; | ||
private readonly LocalizationResourceRepository _repository; | ||
|
||
public CachedLocalizationResourceRepository(LocalizationResourceRepository repository) | ||
{ | ||
_repository = repository; | ||
} | ||
|
||
public string GetTranslation(string key, CultureInfo language) | ||
{ | ||
var cacheKey = BuildCacheKey(key); | ||
var localizationResource = GetFromCache(cacheKey); | ||
if (localizationResource != null) | ||
{ | ||
return localizationResource.Translations.FirstOrDefault(t => t.Language == language.Name)?.Value; | ||
} | ||
|
||
var resource = _repository.GetResource(key); | ||
if (resource == null) | ||
{ | ||
return null; | ||
} | ||
|
||
CacheManager.Insert(cacheKey, resource); | ||
|
||
var localization = resource.Translations.FirstOrDefault(t => t.Language == language.Name); | ||
return localization?.Value; | ||
} | ||
|
||
public IEnumerable<CultureInfo> GetAvailableLanguages() | ||
{ | ||
var cacheKey = BuildCacheKey("AvailableLanguages"); | ||
var cachedLanguages = CacheManager.Get(cacheKey) as IEnumerable<CultureInfo>; | ||
if (cachedLanguages != null) | ||
{ | ||
return cachedLanguages; | ||
} | ||
|
||
var languages = _repository.GetAvailableLanguages(); | ||
CacheManager.Insert(cacheKey, languages); | ||
|
||
return languages; | ||
} | ||
|
||
public IEnumerable<LocalizationResource> GetAllResources() | ||
{ | ||
return _repository.GetAllResources(); | ||
} | ||
|
||
public IEnumerable<ResourceItem> GetAllTranslations(string key, CultureInfo language) | ||
{ | ||
return _repository.GetAllTranslations(key, language); | ||
} | ||
|
||
public void CreateResource(string key, string username) | ||
{ | ||
_repository.CreateResource(key, username); | ||
} | ||
|
||
public void DeleteResource(string key) | ||
{ | ||
_repository.DeleteResource(key); | ||
CacheManager.Remove(BuildCacheKey(key)); | ||
} | ||
|
||
public void CreateOrUpdateTranslation(string key, CultureInfo language, string newValue) | ||
{ | ||
_repository.CreateOrUpdateTranslation(key, language, newValue); | ||
CacheManager.Remove(BuildCacheKey(key)); | ||
} | ||
|
||
public void ClearCache() | ||
{ | ||
if (HttpContext.Current == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (HttpContext.Current.Cache == null) | ||
{ | ||
return; | ||
} | ||
|
||
var itemsToRemove = new List<string>(); | ||
var enumerator = HttpContext.Current.Cache.GetEnumerator(); | ||
|
||
while (enumerator.MoveNext()) | ||
{ | ||
if (enumerator.Key.ToString().ToLower().StartsWith(CacheKeyPrefix.ToLower())) | ||
{ | ||
itemsToRemove.Add(enumerator.Key.ToString()); | ||
} | ||
} | ||
|
||
foreach (var itemToRemove in itemsToRemove) | ||
{ | ||
CacheManager.Remove(itemToRemove); | ||
} | ||
} | ||
|
||
internal void PopulateCache() | ||
{ | ||
var allResources = GetAllResources(); | ||
|
||
ClearCache(); | ||
|
||
foreach (var resource in allResources) | ||
{ | ||
var key = BuildCacheKey(resource.ResourceKey); | ||
CacheManager.Insert(key, resource); | ||
} | ||
} | ||
|
||
internal LanguageEntities GetDatabaseContext() | ||
{ | ||
return _repository.GetDatabaseContext(); | ||
} | ||
|
||
private static string BuildCacheKey(string key) | ||
{ | ||
return $"{CacheKeyPrefix}_{key}"; | ||
} | ||
|
||
private static LocalizationResource GetFromCache(string cacheKey) | ||
{ | ||
var cachedResource = CacheManager.Get(cacheKey); | ||
return cachedResource as LocalizationResource; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using EPiServer.Framework.Localization; | ||
|
||
namespace DbLocalizationProvider | ||
{ | ||
public interface ILocalizationResourceRepository | ||
{ | ||
string GetTranslation(string key, CultureInfo language); | ||
IEnumerable<CultureInfo> GetAvailableLanguages(); | ||
IEnumerable<LocalizationResource> GetAllResources(); | ||
IEnumerable<ResourceItem> GetAllTranslations(string key, CultureInfo language); | ||
void CreateResource(string key, string username); | ||
void DeleteResource(string key); | ||
void CreateOrUpdateTranslation(string key, CultureInfo language, string newValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.