컨트롤러 작업 방법에 대한 출력 캐시를 프로그래밍 방식으로 지우는 방법
컨트롤러 작업에 OutputCache 특성이 지정된 경우 IIS를 다시 시작하지 않고 출력 캐시를 지울 수 있는 방법이 있습니까?
[OutputCache (Duration=3600,VaryByParam="param1;param2")]
public string AjaxHtmlOutputMethod(string param1, string param2)
{
var someModel = SomeModel.Find( param1, param2 );
//set up ViewData
...
return RenderToString( "ViewName", someModel );
}
를 사용하는 중입니다.HttpResponse.RemoveOutputCacheItem(string path)
해결하기 위해서요, 하지만 행동 방법에 매핑하기 위해 어떤 경로가 있어야 하는지 이해하는 데 어려움을 겪고 있습니다.ViewName에서 렌더링하는 aspx 페이지로 다시 시도하려고 합니다.
아마도 수동으로 출력을 삽입할 것입니다.RenderToString
에.HttpContext.Cache
만약 내가 이걸 알아낼 수 없다면 대신에.
갱신하다
출력 캐시는 VariByParam이며 하드 코딩된 경로 "/controller/action"을 테스트해도 실제로 출력 캐시가 지워지지 않으므로 "/controller/action/param1/param2"와 일치해야 합니다.
즉, 객체 레벨 캐싱으로 되돌리고 출력을 수동으로 캐싱해야 합니다.RenderToString()
:(
사용해 보세요.
var urlToRemove = Url.Action("AjaxHtmlOutputMethod", "Controller");
HttpResponse.RemoveOutputCacheItem(urlToRemove);
업데이트됨:
var requestContext = new System.Web.Routing.RequestContext(
new HttpContextWrapper(System.Web.HttpContext.Current),
new System.Web.Routing.RouteData());
var Url = new System.Web.Mvc.UrlHelper(requestContext);
업데이트됨:
사용해 보십시오.
[OutputCache(Location= System.Web.UI.OutputCacheLocation.Server, Duration=3600,VaryByParam="param1;param2")]
그렇지 않으면 사용자의 컴퓨터에서 HTML 출력을 캐시했기 때문에 캐시 삭제가 작동하지 않습니다.
수락된 답변에 추가하여 VariByParam 매개 변수를 지원합니다.
[OutputCache (Duration=3600, VaryByParam="param1;param2", Location = OutputCacheLocation.Server)]
public string AjaxHtmlOutputMethod(string param1, string param2)
{
object routeValues = new { param1 = param1, param2 = param2 };
string url = Url.Action("AjaxHtmlOutputMethod", "Controller", routeValues);
Response.RemoveOutputCacheItem(url);
}
그러나 Egor의 답변은 모든 OutputCacheLocation 값을 지원하기 때문에 훨씬 더 좋습니다.
[OutputCache (Duration=3600, VaryByParam="param1;param2")]
public string AjaxHtmlOutputMethod(string param1, string param2)
{
if (error)
{
Response.Cache.SetNoStore();
Response.Cache.SetNoServerCaching();
}
}
SetNoStore() 및 SetNoServerCaching()을 호출하면 현재 요청이 캐시되지 않습니다.추가 요청에 대해서도 함수가 호출되지 않는 한 추가 요청은 캐시됩니다.
이것은 일반적으로 응답을 캐시하려는 경우 오류 상황을 처리하는 데 이상적이지만 응답에 오류 메시지가 포함된 경우에는 적합하지 않습니다.
올바른 흐름은 다음과 같습니다.
filterContext.HttpContext.Response.Cache.SetNoStore()
AjaxHtmlOutputMethod에 코드 추가
HttpContext.Cache.Insert("Page", 1);
Response.AddCacheItemDependency("Page");
출력 캐시를 지우려면 이제 사용할 수 있습니다.
HttpContext.Cache.Remove("Page");
다른 옵션은 다음과 같습니다.VaryByCustom
OutputCache의 경우 특정 캐시 요소의 무효화를 처리합니다.
당신에게 효과가 있을지도 모르지만, 당신의 문제에 대한 일반적인 해결책은 아닙니다.
언급URL : https://stackoverflow.com/questions/1167890/how-to-programmatically-clear-outputcache-for-controller-action-method
'programing' 카테고리의 다른 글
도커 작성 - 진입점 권한 오류(mariadb) (0) | 2023.06.13 |
---|---|
선택한 시트를 PDF로 내보내려면 Excel VBA (0) | 2023.06.13 |
Oracle: 패키지 내부의 호출 저장 프로시저 (0) | 2023.06.13 |
어떻게 줄리아에게 엑셀을 읽습니까? (0) | 2023.06.13 |
Python에서 반복기의 요소 수 가져오기 (0) | 2023.06.13 |