2010-04-22 2 views
2

OutputCache 지원에 연결하려는 IHttpHandler을 가지고 있으므로 캐시 된 데이터를 IIS 커널로 오프로드 할 수 있습니다. 나는 OutputCacheAttribute이 발견, MVC는 어떻게 든이 작업을 수행해야합니다 알고IHttpHandler를 사용하여 OutputCache를 활성화하는 방법

public override void OnResultExecuting(ResultExecutingContext filterContext) { 
     if (filterContext == null) { 
      throw new ArgumentNullException("filterContext"); 
     } 

     // we need to call ProcessRequest() since there's no other way to set the Page.Response intrinsic 
     OutputCachedPage page = new OutputCachedPage(_cacheSettings); 
     page.ProcessRequest(HttpContext.Current); 
    } 

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

그러나 IHttpHandler이 적용하는 방법을 잘하지. 이런 식으로 뭔가를 시도하지만, 물론이 작동하지 않습니다

public class CacheTest : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     OutputCacheParameters p = new OutputCacheParameters { Duration = 3600, Enabled = true, VaryByParam = "none", Location = OutputCacheLocation.Server }; 
     OutputCachedPage page = new OutputCachedPage(p); 
     page.ProcessRequest(context); 

     context.Response.ContentType = "text/plain"; 
     context.Response.Write(DateTime.Now.ToString()); 
     context.Response.End(); 

    } 

    public bool IsReusable 
    { 
     get 
     { 
      return true; 
     } 
    } 
} 

답변

1

그것은이이 같은 수행 할 :

public class CacheTest : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     TimeSpan expire = new TimeSpan(0, 0, 5, 0); 
     DateTime now = DateTime.Now; 
     context.Response.Cache.SetExpires(now.Add(expire)); 
     context.Response.Cache.SetMaxAge(expire); 
     context.Response.Cache.SetCacheability(HttpCacheability.Server); 
     context.Response.Cache.SetValidUntilExpires(true); 

     context.Response.ContentType = "text/plain"; 
     context.Response.Write(DateTime.Now.ToString()); 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return true; 
     } 
    } 
} 
+0

이 작동하지 않습니다. 이것을 시도하고 페이지를 새로 고침하면 새로 고침 할 때마다 새로운 시간이 표시됩니다. –

+0

죄송합니다. 응답을 꺼냅니다. 끝내 –

+0

IIS 커널 캐시가 누락되었습니다 (perfmon.msc 웹 서비스 캐시 \ Kernel : URI 캐시 누락, 웹 서비스 캐시 \ Kernel : URI 캐시 적중 횟수) –

관련 문제