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

Kie*_*ton 25 asp.net caching ashx

如何使用.ashx处理程序的输出缓存?在这种情况下,我正在进行一些繁重的图像处理,并希望将处理程序缓存一分钟左右.

此外,有没有人有任何关于如何防止堆垛的建议?

Big*_*ing 36

有一些很好的来源,但你想缓存处理服务器端和客户端.

添加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);
Run Code Online (Sandbox Code Playgroud)

至于服务器端缓存,这是一个不同的怪物...并且有很多缓存资源...

  • 我为缩略图做了很多图像服务.您需要注意的一件事是设置最终用户的期望......用户会更新图像,然后去检查实际网站......好吧因为浏览器被告知要缓存图像而不是要求它...他们不会看到更新...(然后他们会哭回到开发......我们会说清楚你的浏览器缓存...) (3认同)

Jef*_*hnn 11

你可以这样使用

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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Fra*_*ega 6

旧的,问题,但答案并没有真正提到服务器端处理.

在获胜的答案中,我会用它来client side:

context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(10)); 
Run Code Online (Sandbox Code Playgroud)

而且server side,因为你使用的是ashx而不是网页,我假设你直接把输出写入了Context.Response.

在这种情况下,你可以使用这样的东西(在这种情况下,我想基于参数"q"保存响应,我使用滑动窗口到期)

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);
}
Run Code Online (Sandbox Code Playgroud)