繁重的自定义路由限制降低了站点的速度

本文关键字:站点 速度 自定义 路由 | 更新日期: 2023-09-27 18:24:59

这种情况并不总是发生,但在大量使用之后,我的网站似乎慢了很多,我唯一更改的代码就是更改重定向。

路由查找旧类别(不再使用),并将其替换为新类别。因此,某人如何访问example.com/scary-shows/buffy将被重定向到example.com/television/bufy

我正试图弄清楚性能损失(如果它发生在这里的话),以及我可以在哪里改进它。

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));
    }
}

繁重的自定义路由限制降低了站点的速度

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

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