How to use output caching on .ashx handler

后端 未结 5 1825
遥遥无期
遥遥无期 2020-12-02 13:40

How can I use output caching with a .ashx handler? In this case I\'m doing some heavy image processing and would like the handler to be cached for a minute or so.

Al

5条回答
  •  一生所求
    2020-12-02 14:23

    you can use like this

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

提交回复
热议问题