나는 우리가 기록을 위해 채택한 해결책으로 내 자신의 질문에 답합니다.
우리는 OutputCacheAttribute
에서 요청 된 URL과 일부 매개 변수에 따라 키가있는 빈 캐시 객체를 추가합니다. 이 옵션은 페이지를 외부 적으로 무효화하는 데 사용됩니다.
그런 다음 현재 요청에 종속되고 이전 cacheKey를 포함하는 키가있는 다른 개체도 추가합니다.
마지막으로 정적 ValidationCallback이 설정됩니다. 콜백은 현재 요청에 대한 키 값 (의존성 키)을 가져옵니다. null이 아닌 경우 종속성의 값을 가져오고, null이면 종속성이 제거되고 validationStatus
을 HttpValidationStatus.Invalid
으로 설정합니다.
일부 코드를 설명하기 :
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
// Build dependencies
BuildDependencies(paramsToDepend, filterContext.Controller, this.Duration);
}
private void BuildDependencies(IEnumerable<string> paramsToDepend, ControllerBase controller, int duration)
{
string[] valuesToInclude = GetValuesToInclude(paramsToInclude, controller.ControllerContext);
// Build the caché key for the current request
var cacheKey = CacheKeyProvider.GetCacheKeyFor(controller, paramsToDepend);
var cache = controller.ControllerContext.HttpContext.Cache;
var cacheValue = cache.Get(cacheKey);
if (cacheValue == null)
{
// The key is created if not exists
Provider.Add(cacheKey, new object(), Context.CurrentDateTime.AddSeconds(duration).ToUniversalTime());
}
// Add the dependency
Provider.Set(CachePrefix + controller.ControllerContext.HttpContext.Request.Path, cacheKey, Context.CurrentDateTime.AddSeconds(duration).ToUniversalTime());
// Register callback
controller.ControllerContext.HttpContext.Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(ValidationCallback), null);
}
public static void ValidationCallback(HttpContext context, object data, ref HttpValidationStatus validationStatus)
{
var provider = OutputCache.Providers[OutputCache.DefaultProviderName];
var dependency = provider.Get(CachePrefix + context.Request.RawUrl) as string;
if (dependency == null) return;
var depValue = provider.Get(dependency);
// If it's null, someone has invelidated the caché (an entity was modified)
if (depValue == null)
{
validationStatus = HttpValidationStatus.Invalid;
}
}