지금 스티브 샌더슨이 his blog에서 만든 아주 좋은 무엇을 사용하고 있습니다 :
이
public class ActionOutputCacheAttribute : ActionFilterAttribute
{
// This hack is optional; I'll explain it later in the blog post
private static readonly MethodInfo _switchWriterMethod = typeof (HttpResponse).GetMethod("SwitchWriter",
BindingFlags.Instance |
BindingFlags.NonPublic);
private readonly int _cacheDuration;
private string _cacheKey;
private TextWriter _originalWriter;
public ActionOutputCacheAttribute(int cacheDuration)
{
_cacheDuration = cacheDuration;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_cacheKey = ComputeCacheKey(filterContext);
var cachedOutput = (string) filterContext.HttpContext.Cache[_cacheKey];
if (cachedOutput != null)
filterContext.Result = new ContentResult {Content = cachedOutput};
else
_originalWriter =
(TextWriter)
_switchWriterMethod.Invoke(HttpContext.Current.Response,
new object[] {new HtmlTextWriter(new StringWriter())});
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
if (_originalWriter != null) // Must complete the caching
{
var cacheWriter =
(HtmlTextWriter)
_switchWriterMethod.Invoke(HttpContext.Current.Response, new object[] {_originalWriter});
string textWritten = (cacheWriter.InnerWriter).ToString();
filterContext.HttpContext.Response.Write(textWritten);
filterContext.HttpContext.Cache.Add(_cacheKey, textWritten, null,
DateTime.Now.AddSeconds(_cacheDuration), Cache.NoSlidingExpiration,
CacheItemPriority.Normal, null);
}
}
private string ComputeCacheKey(ActionExecutingContext filterContext)
{
var keyBuilder = new StringBuilder();
foreach (var pair in filterContext.RouteData.Values)
keyBuilder.AppendFormat("rd{0}_{1}_", pair.Key.GetHashCode(), pair.Value.GetHashCode());
foreach (var pair in filterContext.ActionParameters)
keyBuilder.AppendFormat("ap{0}_{1}_", pair.Key.GetHashCode(), pair.Value.GetHashCode());
return keyBuilder.ToString();
}
}
자세한 내용은 Steve Sanderson blog's article를 방문하십시오.
당신 필 남자, 나는 우아한 해결책이 있어야한다는 것을 알고있었습니다. 감사! –
V2에서이 버그가 수정 되었습니까? – CVertex
@CVertex no. Razor 뷰 엔진을 사용하여 V3에서 수정 될 예정입니까? – stacker