【问题标题】:ASP.Net MVC Custom Route Handler with MemCached带有 MemCached 的 ASP.Net MVC 自定义路由处理程序
【发布时间】:2015-12-20 20:22:06
【问题描述】:

我有一个用 ASP.NET MVC 编写的 CMS。我编写了一个自定义路由处理程序,它查看传入路径并确定将其路由到哪里(即自定义重定向、类别页面、产品页面等)。当前路由存储在 memcached 服务器上的缓存中。一切都很好,直到我们在服务器上获得高负载。然后,我有时会从缓存中检索错误,甚至有时会出现 404 错误。运行需要 5-10 秒的报告也会导致整个服务器挂起

  1. 我是否应该使用自定义路由处理程序开始?
  2. 这不都是异步完成的吗?

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    
    {
        var _lock = new Object();
        MvcHandler handler = null;
        var path = requestContext.HttpContext.Request.Path.ToLower().RemoveOutsideSlashes();
        var qs = requestContext.HttpContext.Request.QueryString;
    
    
        List<Cache.RouteItem> items;
    
        lock (_lock)
        {
            var cache = new CacheManager();
            items = cache.Routes;
        }
    
        if (!hasPermanentRedirect(path, qs, items, ref requestContext))
        {
            // check for categories first
            handler = checkForCategory(path, items, ref requestContext);
    
            // check for product
            if (handler == null) handler = checkForProduct(path, items, ref requestContext);
    
            // check for webpage
            if (handler == null) handler = checkForWebPage(path, items, ref requestContext);
        }
    
        if (handler != null) return handler;
    
        return new MvcHandler(requestContext);
    }
    

【问题讨论】:

  • 我理解对了吗,您自己编写 HttpHandler 而不是在 MVC 堆栈中使用 RouteConfig

标签: c# asp.net-mvc


【解决方案1】:

我会这样做:

  • 我会运行分析器并查看花费最多的时间。这将帮助您了解代码的瓶颈在哪里
  • 我会将你的 CacheManager 类作为静态类,所以你不需要 为每个请求实例化新的 CacheManager。
  • 我会做 checkForCategory、checkForProduct 和 checkForWebPage 方法异步,因此您的代码不包含请求线程,因此您可以 处理更多请求

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2020-04-15
    相关资源
    最近更新 更多