代码之家  ›  专栏  ›  技术社区  ›  Kieran Benton

如何在.ashx处理程序上使用输出缓存

  •  25
  • Kieran Benton  · 技术社区  · 16 年前

    如何将输出缓存与.ashx处理程序一起使用?在本例中,我正在进行一些繁重的图像处理,希望将处理程序缓存一分钟左右。

    另外,有人对如何防止狗食有什么建议吗?

    5 回复  |  直到 8 年前
        1
  •  35
  •   BigBlondeViking    16 年前

    有一些好的源,但您希望缓存处理服务器端和客户端的数据。

    添加HTTP头应该有助于客户端缓存

    下面是一些要开始的响应头..

    你可以花几个小时来调整它们,直到达到你想要的性能。

    //Adds document content type
    context.Response.ContentType = currentDocument.MimeType;
    context.Response.Cache.SetCacheability(HttpCacheability.Public);
    context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
    context.Response.Cache.SetMaxAge(new TimeSpan(0,10,0)); 
    context.Response.AddHeader("Last-Modified", currentDocument.LastUpdated.ToLongDateString());
    
    // Send back the file content
    context.Response.BinaryWrite(currentDocument.Document);
    

    至于服务器端缓存,它是另一个怪物…还有很多缓存资源…

        2
  •  11
  •   Dudu    14 年前

    你可以这样用

    public class CacheHandler : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                OutputCachedPage page = new OutputCachedPage(new OutputCacheParameters
                {
                    Duration = 60,
                    Location = OutputCacheLocation.Server,
                    VaryByParam = "v"
                });
                page.ProcessRequest(HttpContext.Current);
                context.Response.Write(DateTime.Now);
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
            private sealed class OutputCachedPage : Page
            {
                private OutputCacheParameters _cacheSettings;
    
                public OutputCachedPage(OutputCacheParameters cacheSettings)
                {
                    // Tracing requires Page IDs to be unique.
                    ID = Guid.NewGuid().ToString();
                    _cacheSettings = cacheSettings;
                }
    
                protected override void FrameworkInitialize()
                {
                    // when you put the <%@ OutputCache %> directive on a page, the generated code calls InitOutputCache() from here
                    base.FrameworkInitialize();
                    InitOutputCache(_cacheSettings);
                }
            }
        }
    
        3
  •  6
  •   Alex Nolasco    9 年前

    老问题,但答案并没有真正提到服务器端处理。

    就像在获胜的答案中,我会用这个 client side :

    context.Response.Cache.SetCacheability(HttpCacheability.Public);
    context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
    context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(10)); 
    

    为了 server side ,因为您使用的是ashx而不是网页,所以我假设您直接将输出写入 Context.Response .

    在这种情况下,您可以使用类似的方法(在这种情况下,我希望保存基于参数“q”的响应,而im使用滑动窗口过期)。

    using System.Web.Caching;
    
    public void ProcessRequest(HttpContext context)
    {
        string query = context.Request["q"];
        if (context.Cache[query] != null)
        {
            //server side caching using asp.net caching
            context.Response.Write(context.Cache[query]);
            return;
        }
    
        string response = GetResponse(query);   
        context.Cache.Insert(query, response, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10)); 
        context.Response.Write(response);
    }
    
        4
  •  4
  •   John K    15 年前

    我成功地使用了以下内容,并认为值得在这里发布。

    手动控制ASP.NET页输出缓存

    http://dotnetperls.com/cache-examples-aspnet

    在handler.ashx文件中设置缓存选项

    首先,可以使用HTTP处理程序 在ASP.NET中以更快的方式访问服务器 动态内容,而不是Web窗体页。 handler.ashx是的默认名称 ASP.NET通用处理程序。你需要 使用httpContext参数和 以这种方式访问响应。

    示例代码摘录:

    <%@ WebHandler Language="C#" Class="Handler" %>
    

    将响应缓存1小时

    using System;
    using System.Web;
    
    public class Handler : IHttpHandler {
    
        public void ProcessRequest (HttpContext context) {
            // Cache this handler response for 1 hour.
            HttpCachePolicy c = context.Response.Cache;
            c.SetCacheability(HttpCacheability.Public);
            c.SetMaxAge(new TimeSpan(1, 0, 0));
        }
    
        public bool IsReusable {
            get {
                return false;
            }
        }
    }
    
        5
  •  1
  •   knee-cola    8 年前

    解决方案 输出缓存页面 但是,以性能为代价,可以很好地工作,因为您需要实例化从 system.web.ui.page页 基类。

    一个简单的解决方案是使用 响应.cache.setcacheability 正如上面的一些答案所建议的那样。但是,要在服务器上缓存响应(在输出缓存中),需要使用 httpcacheability.server 并设置一个 变字节 ValyByHead (注意使用时 ValyByHead URL不能包含查询字符串,因为将跳过缓存)。

    下面是一个简单的例子(基于 https://support.microsoft.com/en-us/kb/323290 ):

    <%@ WebHandler Language="C#" Class="cacheTest" %>
    using System;
    using System.Web;
    using System.Web.UI;
    
    public class cacheTest : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            TimeSpan freshness = new TimeSpan(0, 0, 0, 10);
            DateTime now = DateTime.Now; 
            HttpCachePolicy cachePolicy = context.Response.Cache;
    
            cachePolicy.SetCacheability(HttpCacheability.Public);
            cachePolicy.SetExpires(now.Add(freshness));
            cachePolicy.SetMaxAge(freshness);
            cachePolicy.SetValidUntilExpires(true);
            cachePolicy.VaryByParams["id"] = true;
    
            context.Response.ContentType = "application/json";
            context.Response.BufferOutput = true;
    
            context.Response.Write(context.Request.QueryString["id"]+"\n");
            context.Response.Write(DateTime.Now.ToString("s"));
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    

    提示: 您监视性能计数器“ASP.NET应用程序总缓存”中的缓存。