Remove RequestCulture caching as perf testing shows it has no benefit

This commit is contained in:
damianedwards 2015-05-11 18:05:13 -07:00
parent 5f12c3b1e3
commit d22adcbef0
7 changed files with 21 additions and 74 deletions

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNet.Localization
public class AcceptLanguageHeaderRequestCultureStrategy : RequestCultureStrategy public class AcceptLanguageHeaderRequestCultureStrategy : RequestCultureStrategy
{ {
/// <summary> /// <summary>
/// The maximum number of values in the Accept-Language header to attempt to create a <see cref="CultureInfo"/> /// The maximum number of values in the Accept-Language header to attempt to create a <see cref="System.Globalization.CultureInfo"/>
/// from for the current request. /// from for the current request.
/// Defaults to <c>3</c>. /// Defaults to <c>3</c>.
/// </summary> /// </summary>
@ -52,7 +52,7 @@ namespace Microsoft.AspNet.Localization
var culture = CultureInfoCache.GetCultureInfo(language.Value); var culture = CultureInfoCache.GetCultureInfo(language.Value);
if (culture != null) if (culture != null)
{ {
var requestCulture = RequestCulture.GetRequestCulture(culture); var requestCulture = new RequestCulture(culture);
requestCulture = ValidateRequestCulture(requestCulture); requestCulture = ValidateRequestCulture(requestCulture);

View File

@ -98,7 +98,7 @@ namespace Microsoft.AspNet.Localization
return null; return null;
} }
return RequestCulture.GetRequestCulture(culture, uiCulture); return new RequestCulture(culture, uiCulture);
} }
} }
} }

View File

@ -67,7 +67,7 @@ namespace Microsoft.AspNet.Localization
return null; return null;
} }
var requestCulture = RequestCulture.GetRequestCulture(culture, uiCulture); var requestCulture = new RequestCulture(culture, uiCulture);
requestCulture = ValidateRequestCulture(requestCulture); requestCulture = ValidateRequestCulture(requestCulture);

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Concurrent;
using System.Globalization; using System.Globalization;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
@ -12,15 +11,24 @@ namespace Microsoft.AspNet.Localization
/// </summary> /// </summary>
public class RequestCulture public class RequestCulture
{ {
private static readonly ConcurrentDictionary<CacheKey, RequestCulture> _cache = new ConcurrentDictionary<CacheKey, RequestCulture>(); /// <summary>
/// Creates a new <see cref="RequestCulture"/> object has its <see cref="Culture"/> and <see cref="UICulture"/>
private RequestCulture([NotNull] CultureInfo culture) /// properties set to the same <see cref="CultureInfo"/> value.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> for the request.</param>
public RequestCulture([NotNull] CultureInfo culture)
: this (culture, culture) : this (culture, culture)
{ {
} }
private RequestCulture([NotNull] CultureInfo culture, [NotNull] CultureInfo uiCulture) /// <summary>
/// Creates a new <see cref="RequestCulture"/> object has its <see cref="Culture"/> and <see cref="UICulture"/>
/// properties set to the respective <see cref="CultureInfo"/> values provided.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> for the request to be used for formatting.</param>
/// <param name="uiCulture">The <see cref="CultureInfo"/> for the request to be used for text, i.e. language.</param>
public RequestCulture([NotNull] CultureInfo culture, [NotNull] CultureInfo uiCulture)
{ {
Culture = culture; Culture = culture;
UICulture = uiCulture; UICulture = uiCulture;
@ -35,66 +43,5 @@ namespace Microsoft.AspNet.Localization
/// Gets the <see cref="CultureInfo"/> for the request to be used for text, i.e. language; /// Gets the <see cref="CultureInfo"/> for the request to be used for text, i.e. language;
/// </summary> /// </summary>
public CultureInfo UICulture { get; } public CultureInfo UICulture { get; }
/// <summary>
/// Gets a cached <see cref="RequestCulture"/> instance that has its <see cref="Culture"/> and <see cref="UICulture"/>
/// properties set to the same <see cref="CultureInfo"/> value.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> for the request.</param>
public static RequestCulture GetRequestCulture([NotNull] CultureInfo culture)
{
return GetRequestCulture(culture, culture);
}
/// <summary>
/// Gets a cached <see cref="RequestCulture"/> instance that has its <see cref="Culture"/> and <see cref="UICulture"/>
/// properties set to the respective <see cref="CultureInfo"/> values provided.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> for the request to be used for formatting.</param>
/// <param name="uiCulture">The <see cref="CultureInfo"/> for the request to be used for text, i.e. language.</param>
/// <returns></returns>
public static RequestCulture GetRequestCulture([NotNull] CultureInfo culture, [NotNull] CultureInfo uiCulture)
{
var key = new CacheKey(culture, uiCulture);
return _cache.GetOrAdd(key, k => new RequestCulture(culture, uiCulture));
}
private class CacheKey
{
private readonly int _hashCode;
public CacheKey(CultureInfo culture, CultureInfo uiCulture)
{
Culture = culture;
UICulture = uiCulture;
_hashCode = new { Culture, UICulture }.GetHashCode();
}
public CultureInfo Culture { get; }
public CultureInfo UICulture { get; }
public bool Equals(CacheKey other)
{
return Culture == other.Culture && UICulture == other.UICulture;
}
public override bool Equals(object obj)
{
var other = obj as CacheKey;
if (other != null)
{
return Equals(other);
}
return base.Equals(obj);
}
public override int GetHashCode()
{
return _hashCode;
}
}
} }
} }

View File

@ -41,12 +41,12 @@ namespace Microsoft.AspNet.Localization
if (Options.SupportedCultures != null && !Options.SupportedCultures.Contains(result.Culture)) if (Options.SupportedCultures != null && !Options.SupportedCultures.Contains(result.Culture))
{ {
result = RequestCulture.GetRequestCulture(Options.DefaultRequestCulture.Culture, result.UICulture); result = new RequestCulture(Options.DefaultRequestCulture.Culture, result.UICulture);
} }
if (Options.SupportedUICultures != null && !Options.SupportedUICultures.Contains(result.UICulture)) if (Options.SupportedUICultures != null && !Options.SupportedUICultures.Contains(result.UICulture))
{ {
result = RequestCulture.GetRequestCulture(result.Culture, Options.DefaultRequestCulture.UICulture); result = new RequestCulture(result.Culture, Options.DefaultRequestCulture.UICulture);
} }
if (requestCulture.Culture != result.Culture && requestCulture.UICulture != result.UICulture) if (requestCulture.Culture != result.Culture && requestCulture.UICulture != result.UICulture)

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Localization
public async Task Invoke([NotNull] HttpContext context) public async Task Invoke([NotNull] HttpContext context)
{ {
var requestCulture = _options.DefaultRequestCulture ?? var requestCulture = _options.DefaultRequestCulture ??
RequestCulture.GetRequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
IRequestCultureStrategy winningStrategy = null; IRequestCultureStrategy winningStrategy = null;

View File

@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Localization
/// </summary> /// </summary>
public RequestLocalizationOptions() public RequestLocalizationOptions()
{ {
DefaultRequestCulture = RequestCulture.GetRequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); DefaultRequestCulture = new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
RequestCultureStrategies = new List<IRequestCultureStrategy> RequestCultureStrategies = new List<IRequestCultureStrategy>
{ {