重定制路由约束减慢站点下降?

时间:2013-07-26 13:35:35

标签: c# asp.net-mvc performance redirect asp.net-mvc-routing

它不会一直发生,但经过一些繁重的使用后,似乎我的网站减速很多,我改变的唯一代码一直在改变重定向。

路由找到旧类别(不再使用)并将其替换为新类别。所以有人如何将example.com/scary-shows/buffy重定向到example.com/television/buffy

我试图找出性能损失(如果它甚至发生在这里),以及我可以改进的地方。

的Global.asax:

...
// custom constraint
    routes.MapRoute("ArticlesCategories", "{category}/{page}",
       new { controller = "Custom", action = "CategoryRedirect" },
       new { isCategory = new CategoryRouteConstraint() });

    //Article routes            
    routes.MapRoute("ArticlesRedirect", "Articles/Redirect",
       new { controller = "_Articles", action = "Redirect" });
             routes.MapRoute("ArticlesSubcategories", "articles/categories/{filter}/{section}",
                    new { controller = "_Articles", action = "Filter", filterBy = "categories", section = UrlParameter.Optional });
          routes.MapRoute("ArticlesTags", "articles/tags/{filter}",
    new { controller = "_Articles", action = "Filter", filterBy = "tags" });
...

约束:

   public class CategoryRouteConstraint:IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
                          RouteDirection routeDirection)
        {
            string page = string.Format("{0}", values["page"]);
            string urlCategory = string.Format("{0}", values["category"]);

            if (GetCategoryDictionary().ContainsKey(urlCategory))
            {
                string topCategory = GetCategoryDictionary()[urlCategory];
                values.Add("topCategory",topCategory);
                return true;
            }
            return false;
        }


        private static List<Category> _categoryList = null;
        private static object _lockObj = new object();
        private static Dictionary<string, string> _dictionary = null;

        private static Dictionary<string, string> GetCategoryDictionary()
        {
            if (_dictionary != null) return _dictionary;

            lock (_lockObj)
            {
                //check again...
                if (_dictionary != null) return _dictionary;
                var dictionary = new Dictionary<string, string>
                    {
                        {"my-old-example-category", "example"},
                        {"blogs", "Blog"},
                        {"doctor-notes", "Blog"},
                        {"action-movies", "movies"},
                        {"reality-tv", "television"},
                        {"scary-shows", "televsion"},
                        ... this goes on for about 100 more categories ...
                    };

                _dictionary = dictionary;
                return dictionary;
            }
        }
    }

控制器:

[OutputCache(Duration = 10800)]
public class CustomController : Controller
{
    public ActionResult CategoryRedirect()
    {
        string topCategory = string.Format("{0}", RouteData.Values["topCategory"]);
        string page = string.Format("{0}",RouteData.Values["page"]);

        return RedirectPermanent(string.Format("/{0}/{1}", topCategory, page));
    }
}

1 个答案:

答案 0 :(得分:0)

您可以稍微优化您的代码:

  • 使用.ToString()而不是string.Format(string.format使用昂贵的正则表达式)
  • 使用TryGetValue()代替您的2个操作
  • 在应用程序启动时初始化字典,或者甚至考虑删除字典,带有返回字符串的大型switch语句比字典更快