Skip to content

Commit

Permalink
Final fixes for v1.0
Browse files Browse the repository at this point in the history
Added cache decorator
  • Loading branch information
valdisiljuconoks committed Feb 28, 2016
1 parent 704281e commit 8befb0b
Show file tree
Hide file tree
Showing 20 changed files with 235 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
<description>$description$</description>
<releaseNotes>Inital version of DbLocalizationProvider AdminUI package for EPiServer</releaseNotes>
<copyright>Copyright 2016 (c) Valdis Iljuconoks, Tech Fellow</copyright>
<tags>Localization EPiServer DbLocalization LocalizationProvider AdminUI UI</tags>
<tags>Localization EPiServer Provider DbLocalization DbLocalizationProvider LocalizationProvider AdminUI UI</tags>
<dependencies>
<dependency id="DbLocalizationProvider" version="1.0.0" />
</dependencies>
</metadata>
<files>
<file src="DbLocalizationProvider.AdminUI.zip" target="content\modules\_protected\DbLocalizationProvider.AdminUI" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class LocalizationResourcesController : Controller

private readonly string _cookieName = ".DbLocalizationProvider-SelectedLanguages";
private readonly ILanguageBranchRepository _languageRepository;
private readonly LocalizationResourceRepository _resourceRepository;
private readonly CachedLocalizationResourceRepository _resourceRepository;

public LocalizationResourcesController(ILanguageBranchRepository languageRepository, LocalizationResourceRepository resourceRepository)
public LocalizationResourcesController(ILanguageBranchRepository languageRepository)
{
_languageRepository = languageRepository;
_resourceRepository = resourceRepository;
_resourceRepository = new CachedLocalizationResourceRepository(new LocalizationResourceRepository());
}

public ActionResult Index()
Expand Down
6 changes: 3 additions & 3 deletions DbLocalizationProvider.AdminUI/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
[assembly: AssemblyTitle("DbLocalizationProvider.AdminUI")]
[assembly: AssemblyDescription("Administration User Interface in EPiServer for DbLocalizationProvider")]
[assembly: Guid("8f0618d8-8200-45e9-9d1e-b268e98e0a84")]
[assembly: AssemblyVersion("0.9.2.0")]
[assembly: AssemblyFileVersion("0.9.2.0")]
[assembly: AssemblyInformationalVersion("0.9.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<description>$description$</description>
<releaseNotes>Inital version of DbLocalizationProvider migration tool for EPiServer</releaseNotes>
<copyright>Copyright 2016 (c) Valdis Iljuconoks, Tech Fellow</copyright>
<tags>Localization EPiServer DbLocalization LocalizationProvider AdminUI UI</tags>
<tags>Localization EPiServer Provider DbLocalization DbLocalizationProvider LocalizationProvider Migration</tags>
</metadata>
<files>
<file src="bin\DbLocalizationProvider.MigrationTool.exe" target="tools\DbLocalizationProvider.MigrationTool.exe" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[assembly: AssemblyTitle("DbLocalizationProvider.MigrationTool")]
[assembly: AssemblyDescription("Migration tool to move Xml resources to database")]
[assembly: Guid("5227c16b-342c-4097-a9ed-295cdd164a61")]
[assembly: AssemblyVersion("0.9.2.0")]
[assembly: AssemblyFileVersion("0.9.2.0")]
[assembly: AssemblyInformationalVersion("0.9.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: InternalsVisibleTo("DbLocalizationProvider.MigrationTool.Tests")]
141 changes: 141 additions & 0 deletions DbLocalizationProvider/CachedLocalizationResourceRepository.cs
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;
}
}
}
8 changes: 8 additions & 0 deletions DbLocalizationProvider/ConfigurationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public class ConfigurationContext

public static ConfigurationContext Current { get; } = new ConfigurationContext();

/// <summary>
/// Gets or sets a value indicating whether Cache should be populated during startup.
/// </summary>
/// <value>
/// <c>true</c> if cache should be populated; otherwise, <c>false</c>.
/// </value>
public bool PopulateCacheOnStartup { get; set; } = true;

public static void Setup(Action<ConfigurationContext> configCallback)
{
configCallback?.Invoke(Current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototyp
metadataFromPrototype.AdditionalValues.Add(keyValuePair.Key, keyValuePair.Value);
}

// handle also case when [Display] attribute is not present
if (metadataFromPrototype.ContainerType != null)
{
prototype.DisplayName = ModelMetadataLocalizationHelper.GetValue(metadataFromPrototype.ContainerType, metadataFromPrototype.PropertyName);
}

return metadataFromPrototype;
}

Expand All @@ -40,6 +34,12 @@ protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IE
prototype.AdditionalValues.Add(validationAttribute.GetHashCode().ToString(CultureInfo.InvariantCulture), validationAttribute.ErrorMessage);
}

// handle also case when [Display] attribute is not present
if (containerType != null)
{
prototype.DisplayName = ModelMetadataLocalizationHelper.GetValue(containerType, propertyName);
}

foreach (var displayAttribute in theAttributes.OfType<DisplayAttribute>().Where(a => !string.IsNullOrWhiteSpace(a.Name)))
{
displayAttribute.Name = ModelMetadataLocalizationHelper.GetValue(containerType, propertyName);
Expand Down
4 changes: 2 additions & 2 deletions DbLocalizationProvider/DatabaseLocalizationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace DbLocalizationProvider
{
public class DatabaseLocalizationProvider : LocalizationProvider
{
private readonly LocalizationResourceRepository _repository;
private readonly CachedLocalizationResourceRepository _repository;

public DatabaseLocalizationProvider()
{
_repository = new LocalizationResourceRepository();
_repository = new CachedLocalizationResourceRepository(new LocalizationResourceRepository());
}

public override IEnumerable<CultureInfo> AvailableLanguages => _repository.GetAvailableLanguages();
Expand Down
2 changes: 2 additions & 0 deletions DbLocalizationProvider/DbLocalizationProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@
<Compile Include="..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="CachedLocalizationResourceRepository.cs" />
<Compile Include="DataAnnotations\CachedLocalizedMetadataProvider.cs" />
<Compile Include="DataAnnotations\LocalizedMetadataProvider.cs" />
<Compile Include="DataAnnotations\LocalizedModelValidatorProvider.cs" />
<Compile Include="DataAnnotations\ModelMetadataLocalizationHelper.cs" />
<Compile Include="ILocalizationResourceRepository.cs" />
<Compile Include="LocalizationServiceExtensions.cs" />
<Compile Include="LocalizationResourceRepository.cs" />
<Compile Include="ExpressionHelper.cs" />
Expand Down
2 changes: 1 addition & 1 deletion DbLocalizationProvider/DbLocalizationProvider.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<description>$description$</description>
<releaseNotes>Inital version of DbLocalizationProvider package for EPiServer</releaseNotes>
<copyright>Copyright 2016 (c) Valdis Iljuconoks, Tech Fellow</copyright>
<tags>Localization EPiServer DbLocalization LocalizationProvider</tags>
<tags>Localization EPiServer Provider DbLocalization DbLocalizationProvider LocalizationProvider</tags>
</metadata>
<files>
<file src="readme.txt" target="readme.txt" />
Expand Down
2 changes: 0 additions & 2 deletions DbLocalizationProvider/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ internal static Stack<MemberInfo> WalkExpression(LambdaExpression expression)
e = null;
break;
case ExpressionType.Parameter:
//var parameterExpr = (ParameterExpression) e;
//stack.Push(parameterExpr.Type);
e = null;
break;
default:
Expand Down
5 changes: 5 additions & 0 deletions DbLocalizationProvider/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace DbLocalizationProvider
{
public static class HtmlHelperExtensions
{
//public static MvcHtmlString Translate<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, object>> model, params object[] formatArguments)
//{
// return Translate(helper, model.Body, formatArguments);
//}

public static MvcHtmlString Translate(this HtmlHelper helper, Expression<Func<object>> model, params object[] formatArguments)
{
if (model == null)
Expand Down
17 changes: 17 additions & 0 deletions DbLocalizationProvider/ILocalizationResourceRepository.cs
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);
}
}
4 changes: 2 additions & 2 deletions DbLocalizationProvider/Import/ResourceImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace DbLocalizationProvider.Import
{
public class ResourceImporter
{
private readonly LocalizationResourceRepository _resourceRepository;
private readonly CachedLocalizationResourceRepository _resourceRepository;

public ResourceImporter(LocalizationResourceRepository resourceRepository)
public ResourceImporter(CachedLocalizationResourceRepository resourceRepository)
{
_resourceRepository = resourceRepository;
}
Expand Down
Loading

0 comments on commit 8befb0b

Please sign in to comment.