在ashx中使用aspx的OutputCached

 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);
            }
        }
    }
posted @ 2011-06-04 12:45  MicroCoder  阅读(540)  评论(0编辑  收藏  举报