Skip to content

Commit

Permalink
Performance enhancements for #380 fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
NightOwl888 committed Jan 30, 2015
1 parent b0e3105 commit b4929b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static void AddWithCaseCorrection(this NameValueCollection nameValueColle
{
// Add item with corrected case key
nameValueCollection.Add(item, value);
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using MvcSiteMapProvider.Caching;
using MvcSiteMapProvider.Globalization;
using MvcSiteMapProvider.Web.Mvc;
Expand Down Expand Up @@ -167,6 +170,27 @@ public override string Route
set { this.SetCachedOrMemberValue<string>(x => base.Route = x, "Route", value); }
}

protected override NameValueCollection GetCaseCorrectedQueryString(HttpContextBase httpContext)
{
// This method is called twice per node only in the case where there are
// preserved route parameters, so the memory trade-off is only worth it if
// we have some configured.
if (this.PreservedRouteParameters.Any())
{
var key = this.GetCacheKey("GetCaseCorrectedQueryString_" + httpContext.Request.Url.Query);
var result = this.requestCache.GetValue<NameValueCollection>(key);
if (result == null)
{
result = base.GetCaseCorrectedQueryString(httpContext);
this.requestCache.SetValue<NameValueCollection>(key, result);
}

return result;
}

return base.GetCaseCorrectedQueryString(httpContext);
}

protected override bool AreRouteParametersPreserved
{
get
Expand Down
12 changes: 7 additions & 5 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
Expand Down Expand Up @@ -645,7 +646,7 @@ protected virtual void PreserveRouteParameters()

/// <summary>
/// Gets a <see cref="T:System.Collections.Specialized.NameValueCollection"/> containing the query string
/// key value pairs for the passed in HTTP context. The casing of the keys corrected to be the same as the values that are
/// key value pairs for the passed in HTTP context. The casing of the keys corrected to be the same case as the values that are
/// configured either in the <see cref="P:RouteValues"/> dictionary or the <see cref="P:PreservedRouteParameters"/> collection.
/// </summary>
/// <param name="httpContext">The HTTP context.</param>
Expand All @@ -656,9 +657,10 @@ protected virtual NameValueCollection GetCaseCorrectedQueryString(HttpContextBas
var queryStringValues = httpContext.Request.QueryString;
// Note: we must use the configured route values, rather than the RouteValue property to avoid an
// infinite loop.
var caseInsensitiveRouteKeys = new HashSet<string>(this.routeValues.Keys, StringComparer.InvariantCultureIgnoreCase);
var routeKeys = this.routeValues.Keys.ToArray();
var caseInsensitiveRouteKeys = new HashSet<string>(routeKeys, StringComparer.InvariantCultureIgnoreCase);
var caseInsensitivePreservedRouteParameters = new HashSet<string>(this.PreservedRouteParameters, StringComparer.InvariantCultureIgnoreCase);
var result = new NameValueCollection();
var result = new NameValueCollection(queryStringValues.Count);

foreach (var key in queryStringValues.AllKeys)
{
Expand All @@ -667,11 +669,11 @@ protected virtual NameValueCollection GetCaseCorrectedQueryString(HttpContextBas
{
if (caseInsensitivePreservedRouteParameters.Contains(key))
{
result.AddWithCaseCorrection(key, queryStringValues[key], caseInsensitivePreservedRouteParameters);
result.AddWithCaseCorrection(key, queryStringValues[key], this.PreservedRouteParameters);
}
else if (caseInsensitiveRouteKeys.Contains(key))
{
result.AddWithCaseCorrection(key, queryStringValues[key], caseInsensitiveRouteKeys);
result.AddWithCaseCorrection(key, queryStringValues[key], routeKeys);
}
else
{
Expand Down

0 comments on commit b4929b9

Please sign in to comment.